New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customisable validation #79
Comments
My first reaction is that the type check probably should result in an validation error if the validator for that type could not be found. On the other hand some other errors like missing schema URI's get ignored too. Many of the extra JS properties are non-exclusive additions to regular rules so they might still need their normal types for the regular validation rules, so I'm not sure if this should be toggled by the type value. In a way it these ideas are a little like the "format" of the string values. I'll have to brood on this some more. |
OK, I've put some stuff in a new branch - I'd appreciate any feedback you have. Custom types are not handled, but custom keywords can be defined:
The new methods are defined in the branch's README. |
Thanks for that feature. Considering a use case for a {
properties: {
title: { type: 'string', unique: true }
},
// Or in root (the same as having a parent array schema with `uniqueItems: true`):
unique: true,
// Or as a list of unique properties:
unique: ['title']
} 1) Asynchronous validation tv4.defineKeyword('unique', function (data, value, schema, done) {
// do some lookups, then
done({code: .., message: ..})
}) 2) Providing a context during validation, perhaps as a custom option. tv4.defineKeyword('unique', function (data, value, schema, opts) {
if (Array.isArray(value)) {
// value is an array of properties that should be unique
opts.context.find(..)
} else if (value === true) {
// Either a property should be unique, or the entity as a whole
opts.context.find(..)
}
})
tv4.validateMultiple(someEntity, schema, {context: someCollection}) 3) Knowing the keyword's position by providing a |
The issue with (1) is that it would break the existing API - to allow asynchronous custom validation, then we'd have to make validation asynchronous through and through, and end up removing the asynchronous API (or doubling code size by having two versions). I don't quite understand (2) - what are you finding? What data/methods are encapsulated in I also don't understand (3) - are you saying that if you took your example and embedded it in another schema like |
|
OK - with (3), I think that a keyword that depends on document position for its behaviour is intensely problematic. Currently, for all possible values for Schema A, the following two are equivalent: {"id": "/schemas/A", ... Schema A ...}
{
"id": "/schemas/B",
"properties": {
"a": {"$ref": "/schemas/A"}
}
} and: {
"id": "/schemas/B",
"properties": {
"a": {... Schema A ...}
}
} Having a keyword that behaved differently just because it was in the root of a document would violate that principle. So, I'm afraid I'm not particularly keen to support positional information based on this use-case, because I think it would be better to redefine the |
Yes, very much. Thanks for helping me understand that principle. I see the value in it. It's better to just do (similar to {
"unique": ["title", ..],
"properties": {
"title": {..}
}
} |
Cool - so is it OK to close this issue? |
👍 |
I implemented an async format validation in my fork. blackmiaool@7f3c69d |
I think this could be done with a combination of extra types, and simple "validation extension" functions to be called each time.
From @Bartvds:
Using json-schema on JS objects could use a few more schema rules/flags. Like
ownProperty
,enumerable
function
,date
,regex
, maybenan
,undefined
etc)frozen
/sealed
/extensible
So maybe we should look at the schema-rule part of this in a slightly wider scope and see if
tv4
+ json-schema can support these kind of JS specific 'extensions' (I guess it can if the implementation complexity is not too much).The text was updated successfully, but these errors were encountered: