From 2db9c58ba1c7d17687963a015d83a557d430e3a8 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Tue, 1 Nov 2022 16:58:07 -0400 Subject: [PATCH] Update the Draft 3 and 4 metaschemas. 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@cf0ec7210011272f4e95f70f8c1374593e13c8c5) as well as https://github.com/json-schema-org/json-schema-spec/issues/717#issuecomment-1299112660 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 --- jsonschema/schemas/draft3.json | 19 +++++++------------ jsonschema/schemas/draft4.json | 8 ++++---- jsonschema/tests/test_validators.py | 22 ++++++++++++++++++++-- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/jsonschema/schemas/draft3.json b/jsonschema/schemas/draft3.json index 23d59b640..8b26b1f89 100644 --- a/jsonschema/schemas/draft3.json +++ b/jsonschema/schemas/draft3.json @@ -15,7 +15,7 @@ "properties" : { "type" : "object", - "additionalProperties" : {"$ref" : "#", "type" : "object"}, + "additionalProperties" : {"$ref" : "#"}, "default" : {} }, @@ -47,7 +47,7 @@ }, "dependencies" : { - "type" : ["string", "array", "object"], + "type" : "object", "additionalProperties" : { "type" : ["string", "array", {"$ref" : "#"}], "items" : { @@ -75,11 +75,6 @@ "default" : false }, - "maxDecimal": { - "minimum": 0, - "type": "number" - }, - "minItems" : { "type" : "integer", "minimum" : 0, @@ -112,7 +107,9 @@ }, "enum" : { - "type" : "array" + "type" : "array", + "minItems" : 1, + "uniqueItems" : true }, "default" : { @@ -153,13 +150,11 @@ }, "id" : { - "type" : "string", - "format" : "uri" + "type" : "string" }, "$ref" : { - "type" : "string", - "format" : "uri" + "type" : "string" }, "$schema" : { diff --git a/jsonschema/schemas/draft4.json b/jsonschema/schemas/draft4.json index ba0c11708..bcbb84743 100644 --- a/jsonschema/schemas/draft4.json +++ b/jsonschema/schemas/draft4.json @@ -28,12 +28,10 @@ "type": "object", "properties": { "id": { - "format": "uri", "type": "string" }, "$schema": { - "type": "string", - "format": "uri" + "type": "string" }, "title": { "type": "string" @@ -122,7 +120,9 @@ } }, "enum": { - "type": "array" + "type": "array", + "minItems": 1, + "uniqueItems": true }, "type": { "anyOf": [ diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index ea7d3e476..ea25f7860 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -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):