Skip to content

Commit

Permalink
Update the Draft 3 and 4 metaschemas.
Browse files Browse the repository at this point in the history
For draft 4 this pulls in upstream fixes which were not present locally,
notably fixing `id` to not have `format: uri` in it, because location
independent identifiers are indeed not URIs (they're URI references as
later metaschemas use).

For enum, on draft 3 and 4, this also *re-adds* constraints that enum
items MUST be unique and the array non-empty. In later drafts, this
restriction was loosened (see
json-schema-org/json-schema-spec@cf0ec72)
as well as json-schema-org/json-schema-spec#717 (comment)
but in drafts 3 and 4 it is present.

The draft 4 metaschema contains these assertions, the draft 3 one is
still buggy and does not, so they're just applied locally here.

Ref: json-schema-org/json-schema-spec#310
  • Loading branch information
Julian committed Nov 1, 2022
1 parent f40199d commit 2db9c58
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
19 changes: 7 additions & 12 deletions jsonschema/schemas/draft3.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

"properties" : {
"type" : "object",
"additionalProperties" : {"$ref" : "#", "type" : "object"},
"additionalProperties" : {"$ref" : "#"},
"default" : {}
},

Expand Down Expand Up @@ -47,7 +47,7 @@
},

"dependencies" : {
"type" : ["string", "array", "object"],
"type" : "object",
"additionalProperties" : {
"type" : ["string", "array", {"$ref" : "#"}],
"items" : {
Expand Down Expand Up @@ -75,11 +75,6 @@
"default" : false
},

"maxDecimal": {
"minimum": 0,
"type": "number"
},

"minItems" : {
"type" : "integer",
"minimum" : 0,
Expand Down Expand Up @@ -112,7 +107,9 @@
},

"enum" : {
"type" : "array"
"type" : "array",
"minItems" : 1,
"uniqueItems" : true
},

"default" : {
Expand Down Expand Up @@ -153,13 +150,11 @@
},

"id" : {
"type" : "string",
"format" : "uri"
"type" : "string"
},

"$ref" : {
"type" : "string",
"format" : "uri"
"type" : "string"
},

"$schema" : {
Expand Down
8 changes: 4 additions & 4 deletions jsonschema/schemas/draft4.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
"type": "object",
"properties": {
"id": {
"format": "uri",
"type": "string"
},
"$schema": {
"type": "string",
"format": "uri"
"type": "string"
},
"title": {
"type": "string"
Expand Down Expand Up @@ -122,7 +120,9 @@
}
},
"enum": {
"type": "array"
"type": "array",
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
Expand Down
22 changes: 20 additions & 2 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1453,17 +1453,35 @@ def test_enum_allows_empty_arrays(self):
"""
Technically, all the spec says is they SHOULD have elements, not MUST.
(As of Draft 6. Previous drafts do say MUST).
See #529.
"""
self.Validator.check_schema({"enum": []})
if self.Validator in {
validators.Draft3Validator,
validators.Draft4Validator,
}:
with self.assertRaises(exceptions.SchemaError):
self.Validator.check_schema({"enum": []})
else:
self.Validator.check_schema({"enum": []})

def test_enum_allows_non_unique_items(self):
"""
Technically, all the spec says is they SHOULD be unique, not MUST.
(As of Draft 6. Previous drafts do say MUST).
See #529.
"""
self.Validator.check_schema({"enum": [12, 12]})
if self.Validator in {
validators.Draft3Validator,
validators.Draft4Validator,
}:
with self.assertRaises(exceptions.SchemaError):
self.Validator.check_schema({"enum": [12, 12]})
else:
self.Validator.check_schema({"enum": [12, 12]})


class ValidatorTestMixin(MetaSchemaTestsMixin, object):
Expand Down

0 comments on commit 2db9c58

Please sign in to comment.