tv4 icon indicating copy to clipboard operation
tv4 copied to clipboard

required must be a non-empty array

Open JKDingwall opened this issue 11 years ago • 4 comments

If a schema has an empty require array then it is not valid. The draft-4 schema defines require as:

"required": { "$ref": "#/definitions/stringArray" },

"stringArray": { "items": { "type": "string" }, "minItems": 1, "type": "array", "uniqueItems": true }

i.e. this should be an invalid schema:

{ "$schema": "http://json-schema.org/draft-04/schema#", "description": "Inline validation schema", "id": "#", "properties": { "planet": { "type": "string", "pattern": "^earth$", "description": "Our place in the big wilderness of The Universe" } }, "additionalProperties": false, "required": [ ] } (Sorry if this is a dupe, it didn't seem to immediately appear in the list after the first submit so done again.)

JKDingwall avatar Feb 10 '14 16:02 JKDingwall

I have created a pull request for this which contains a fix that works but perhaps you prefer a different implementation.

JKDingwall avatar Feb 10 '14 16:02 JKDingwall

So you're expecting an error if a schema has an empty array for required?

tv4 is generally extremely lenient in what it accepts as a schema, because it's not the schema that's actually being validated. There are a few other issues surrounding making sure the schema is sane (#59, #72), and this could be part of those.

Generally, though, I'm not sure how much benefit there is in throwing an error for a fairly harmlessly incorrect schema. Zero-length required values seem pretty benign to me...

geraintluff avatar Feb 12 '14 19:02 geraintluff

I encountered this problem by validating a schema I had written against the JSON v4 schema. Other validators would raise an exception against this whereas tv4 did not. I can understand trying to be lenient but it is an error against the core schema. Perhaps tv4 should offer a strict/lenient mode?

JKDingwall avatar Feb 13 '14 08:02 JKDingwall

OK, I'll be honest - the requirement that required must have at least one value is pointless, and I see no benefit in enforcing it. :p If required is empty, the intention of the schema is still clear (no required properties) so I think validation should continue.

However, if people want to validate that their schemas are fully compliant, then I agree we should probably provide that option. I reckon the best way to do that is to validate against the meta-schemas, so we should make sure that works.

geraintluff avatar Feb 13 '14 14:02 geraintluff