Comments (6)
What is a union properties
? I don't understand what you are trying to say.
from revalidator.
Is it this?
{"type":["string","number"]}
The property can be either string or number?
from revalidator.
Hi PK, Marak, sorry about the vague bug report. Here is some more context.
Consider a Post
that can optionally have a source which is defined by a schema itself. Basically, I want to validate some json such that the source property is either not present (null
) or conforms to a schema. To achieve this, I referred to section 5.1 of the draft.
Here is an example json-schema (v3) to describe a Post
:
{
"type": "object",
"properties": {
"title": {
"type": "string",
"required": true
},
"body": {
"type": "string",
"required": true
},
"source": {
"type": [
{
"type": "null"
}, {
"type": "object",
"properties": {
"name": {
"type": "string",
"required": true
},
"url": {
"type": "string",
"required": true
}
}
]
}
}
}
from revalidator.
I see. If you need a 100% compliant validator, you might want to check out https://github.com/garycourt/JSV
from revalidator.
Hi Marak, this is what I'm using and seems to be more or less API compatible: https://github.com/kriszyp/json-schema/blob/master/lib/validate.js
from revalidator.
I think this is already supported. Here's some code:
var revalidator = require('revalidator');
var schema = {
properties: {
title: {
type: 'string',
required: true
},
body: {
type: 'string',
required: true
},
source: {
type: ['object', 'null'],
properties: {
name: {
type: 'string',
required: true
},
url: {
type: 'string',
required: true
}
}
}
}
};
// good: missing, null, or conforming to schema.
var object1 = {
title: 'a title',
body: 'this is the body'
}, object2 = {
title: 'a title',
body: 'this is the body',
source: null
}, object3 = {
title: 'a title',
body: 'this is the body',
source: {
name: 'foo bar',
url: 'http://www.example.com'
}
};
// bad: empty, not conforming to schema.
var object4 = {
title: 'a title',
body: 'this is the body',
source: {}
}, object5 = {
title: 'a title',
body: 'this is the body',
source: {
name: 'foo bar',
a: 1337
}
}, object6 = {
title: 'a title',
body: 'this is the body',
source: {
a: 1337,
url: 'http://www.example.com'
}
};
console.log(revalidator.validate(object1, schema));
console.log(revalidator.validate(object2, schema));
console.log(revalidator.validate(object3, schema));
console.log(revalidator.validate(object4, schema));
console.log(revalidator.validate(object5, schema));
console.log(revalidator.validate(object6, schema));
Output:
$ node opt
{ valid: true, errors: [] }
{ valid: true, errors: [] }
{ valid: true, errors: [] }
{ valid: false,
errors:
[ { attribute: 'required',
property: 'source.name',
expected: true,
actual: undefined,
message: 'is required' },
{ attribute: 'required',
property: 'source.url',
expected: true,
actual: undefined,
message: 'is required' } ] }
{ valid: false,
errors:
[ { attribute: 'required',
property: 'source.url',
expected: true,
actual: undefined,
message: 'is required' } ] }
{ valid: false,
errors:
[ { attribute: 'required',
property: 'source.name',
expected: true,
actual: undefined,
message: 'is required' } ] }
$
I think this issue can be closed as well.
from revalidator.
Related Issues (20)
- source missing license header HOT 1
- allowEmpty true, minLength > 0 throws a validation error HOT 2
- Crashes when 'additionalProperties' is a schema. HOT 1
- Object validation throwing error HOT 1
- Array validation HOT 3
- ensure property is not present
- undefined passes enum test HOT 2
- Can lock up system with malicious email strings
- Input for type 'number' divisibleBy issue (2.99 not divisible by 0.01)
- Validation array of objects
- Validate key without making it required?
- type validation with object throws error message
- It doesn't really check if the value passed is an object
- Decimal Digits validation fails with type number
- Validating a revalidator schema
- Maintaining revalidator HOT 4
- security contact
- GHSL-2020-352: seeking security contact for your project
- Export types and interfaces
- vulnerability in revalidator
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 revalidator.