Comments (9)
Hi @burhanfarooq , you can do it like this (see 8c4a6e3)
var schemaForObject = {
type: "object",
additionalProperties: false,
properties: {
"is_and": { type: "boolean" },
"filters": {
type: "array",
additionalItems: false,
items: {
oneOf: [
{ $ref: "#" },
{ $ref: "#/definitions/last" }
]
}
}
},
definitions: {
"last": {
type: "object",
additionalProperties: false,
properties: {
"text": { type: "string" },
"is_last": { type: "boolean" },
"filters": { type: "array", additionalItems: false }
}
}
}
};
from z-schema.
@zaggino thanks
from z-schema.
one more question now the error message i am getting in case of wrong property name or mismatch type value of property is not exactly pin pointing the property it just say something like that:
{
"instance": {
"is_and": false,
"filters": [
{
"is_and": false,
"filters": [
{
"is_and": true,
"filters": [
{
"is_and": true,
"filters": [
{
"is_and": true,
"filters": [
{
"is_and": true,
"filters": [
{
"text": 99,
"is_last": true,
"filters": []
}
]
}
]
}
]
}
]
}
]
}
]
},
"schema": {
"type": "object",
"required": true,
"additionalProperties": false,
"properties": {
"is_and": {
"type": "boolean",
"required": true
},
"filters": {
"type": "array",
"additionalItems": false,
"items": {
"oneOf": [
{
"$ref": "#"
},
{
"$ref": "#/definitions/last"
}
]
}
}
},
"definitions": {
"last": {
"type": "object",
"required": true,
"additionalProperties": false,
"properties": {
"text": {
"type": "string"
},
"is_last": {
"type": "boolean"
},
"filters": {
"type": "array",
"additionalItems": false
}
}
}
}
},
"propertyPath": "my_object",
"errors": [
{
"property": "my_object.filters[0]",
"message": "is not exactly one from [object Object],[object Object]",
"schema": {
"oneOf": [
{
"$ref": "#"
},
{
"$ref": "#/definitions/last"
}
]
},
"instance": {
"is_and": false,
"filters": [
{
"is_and": true,
"filters": [
{
"is_and": true,
"filters": [
{
"is_and": true,
"filters": [
{
"is_and": true,
"filters": [
{
"text": 99,
"is_last": true,
"filters": []
}
]
}
]
}
]
}
]
}
]
},
"stack": "my_object.filters[0] is not exactly one from [object Object],[object Object]"
},
{
"property": "my_object.filters[0]",
"message": "is not exactly one from [object Object],[object Object]",
"schema": {
"oneOf": [
{
"$ref": "#"
},
{
"$ref": "#/definitions/last"
}
]
},
"instance": {
"is_and": false,
"filters": [
{
"is_and": true,
"filters": [
{
"is_and": true,
"filters": [
{
"is_and": true,
"filters": [
{
"is_and": true,
"filters": [
{
"text": 99,
"is_last": true,
"filters": []
}
]
}
]
}
]
}
]
}
]
},
"stack": "my_object.filters[0] is not exactly one from [object Object],[object Object]"
}
]
}
the text property in of last object is number instead of string,
normally the validation points to nested arrays with index and property name but not in this case???
from z-schema.
Please edit you message to fix formatting of the code - it's unreadable.
https://help.github.com/articles/github-flavored-markdown
from z-schema.
@zaggino code updated have look now please
from z-schema.
@burhanfarooq this will require a validator fix, i'll look at it soon
from z-schema.
Roger SIr :)
from z-schema.
Validate like this
ZSchema.validate(invalidObject, schemaForObject, function (err) {
console.log(JSON.stringify(err, null, 4));
});
You'll get an error like this one, which is fine and can't be really done any better.
Path is good - because there is oneOf, validator needs to go both ways validating complete object below current one.
Reasons why oneOf schemas didn't pass on this level are logged into array subErrors,
When you look at the deepest error you find what you've been looking for
"code": "INVALID_TYPE",
"message": "invalid type: integer (expected string)",
"path": "#/filters/[0]/text",
{
"errors": [
{
"code": "ONE_OF_MISSING",
"message": "Data does not match any schemas from \"oneOf\"",
"path": "#/filters/[0]",
"params": {},
"subErrors": [
{
"code": "OBJECT_ADDITIONAL_PROPERTIES",
"message": "Additional properties not allowed",
"path": "#/filters/[0]",
"params": {
"properties": [
"is_and"
]
}
},
{
"code": "ONE_OF_MISSING",
"message": "Data does not match any schemas from \"oneOf\"",
"path": "#/filters/[0]/filters/[0]",
"params": {},
"subErrors": [
{
"code": "OBJECT_ADDITIONAL_PROPERTIES",
"message": "Additional properties not allowed",
"path": "#/filters/[0]",
"params": {
"properties": [
"is_and"
]
}
},
{
"code": "ONE_OF_MISSING",
"message": "Data does not match any schemas from \"oneOf\"",
"path": "#/filters/[0]/filters/[0]",
"params": {},
"subErrors": [
{
"code": "OBJECT_ADDITIONAL_PROPERTIES",
"message": "Additional properties not allowed",
"path": "#/filters/[0]",
"params": {
"properties": [
"is_and"
]
}
},
{
"code": "ONE_OF_MISSING",
"message": "Data does not match any schemas from \"oneOf\"",
"path": "#/filters/[0]/filters/[0]",
"params": {},
"subErrors": [
{
"code": "OBJECT_ADDITIONAL_PROPERTIES",
"message": "Additional properties not allowed",
"path": "#/filters/[0]",
"params": {
"properties": [
"is_and"
]
}
},
{
"code": "ONE_OF_MISSING",
"message": "Data does not match any schemas from \"oneOf\"",
"path": "#/filters/[0]/filters/[0]",
"params": {},
"subErrors": [
{
"code": "OBJECT_ADDITIONAL_PROPERTIES",
"message": "Additional properties not allowed",
"path": "#/filters/[0]",
"params": {
"properties": [
"is_and"
]
}
},
{
"code": "ONE_OF_MISSING",
"message": "Data does not match any schemas from \"oneOf\"",
"path": "#/filters/[0]/filters/[0]",
"params": {},
"subErrors": [
{
"code": "OBJECT_ADDITIONAL_PROPERTIES",
"message": "Additional properties not allowed",
"path": "#/filters/[0]",
"params": {
"properties": [
"text",
"is_last"
]
}
},
{
"code": "INVALID_TYPE",
"message": "invalid type: integer (expected string)",
"path": "#/filters/[0]/text",
"params": {
"expected": "string",
"type": "integer"
}
}
]
}
]
}
]
}
]
}
]
}
]
}
],
"warnings": []
}
from z-schema.
@zaggino
thanks (Y)
from z-schema.
Related Issues (20)
- add keyword in error objects of getLastErrors()
- Allow to specify errors to be reported
- why when schema type is string, there is no error when the value type is integer or number ? HOT 2
- Update to core-js 3.x
- Doesn't validate minimum and maximum on numbers HOT 1
- Versions mismatch cause npm error HOT 4
- Remove core-js HOT 2
- Missing type for Validator.validate(json, schema, options) HOT 3
- 'email' format accepts email addresses with several types of whitespace. HOT 3
- 'ARRAY_ADDITIONAL_ITEMS' error unreachable?
- Is there any approach to suppress an error code HOT 2
- Local schemas not being retrieved from cache
- zSchema doesn't support json-schema post 2020-12
- Vulnerabilities introduced by package validator HOT 2
- Vulnerability in Validator JS HOT 1
- Update Validator to 13.7.0 HOT 1
- `lastReport` absent in `index.d.ts` HOT 2
- Update commander as it's version marked as EOL
- Support for Unicode Properties
- List on json-schema.org HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from z-schema.