Open jasonkarns opened 7 years ago
Thanks for the report. I'll see what I can do.
@jasonkarns, this is a usage issue (which may also indicate a design problem). I've modified your example to make it work:
jsck = new JSCK.draft4
id: "urn:some.id#"
type: "object"
properties:
user:
type: "object"
required: ["login"]
properties:
login:
type: "string"
pattern: "^[\\w\\d_]{3,32}$"
email:
type: "string"
format: "email"
validator = jsck.validator(uri: "urn:some.id#")
validator.validate
user:
login: "matthew"
email: "matthew@pandastrike.com"
An instance of JSCK may contain numerous schemas, all with different id
s. The README example you worked with demonstrates the simple, single-schema usage. When there may be more than one schema involved, the current design requires that you specify which schema you want to validate a document with.
@automatthew can we improve the docs on that?
We can and should.
@automatthew or if we don't see a URI, can we simply assume there's only one id
and use that?
Yeah, maybe with this logic:
id
: the JSCK instance is a singletary (neologisms ftw)id
: current behaviorPart of this is still throwing me for a loop. I understand current behavior, but note in my example I'm only constructing the validator with a single schema. Presence of an ID in the schema does not change the fact that the validator still only has a single schema and should operate as such.
Basically, current behavior is saying that the content of a schema will alter the acceptable usage of the validator. And that's a flaw, IMO.
I agree. I'll be altering it per my comment just above.
Another option I considered is having two different validation classes: one for the case where you are using a single schema, and another for when you want the validator to have access to several. That is arguably more semantically accurate, but probably even more confusing.
For most of our use cases (and it seems also for yours) we don't need to have multiple schema documents within a single validator. We use #/definitions/
and JSON pointers as needed.
Sample taken directly from the Readme, with an
id
added to the schema:This throws the error:
Based on my debugging, this occurs because a schema's
id
is used as the key for that schema. If no id is provided, then the schema is indexed with#
as the key. https://github.com/pandastrike/jsck/blob/fdc93ac2bcecc8bf10463b5b51569fa7219620aa/src/validator.coffee#L78-L80Then when validation is attempted, the validate call just blindly uses
'#'
as the key to lookup the schema: https://github.com/pandastrike/jsck/blob/fdc93ac2bcecc8bf10463b5b51569fa7219620aa/src/validator.coffee#L86If the schema in question has an id, then the schema lookup fails to find one indexed under
'#'
, and throws the error: https://github.com/pandastrike/jsck/blob/fdc93ac2bcecc8bf10463b5b51569fa7219620aa/src/validator.coffee#L107