diff --git a/spec/fixtures/expected-schema-from-draft-04-to-07.json b/spec/fixtures/expected-schema-from-draft-04-to-07.json index abad5e1..6ba8249 100644 --- a/spec/fixtures/expected-schema-from-draft-04-to-07.json +++ b/spec/fixtures/expected-schema-from-draft-04-to-07.json @@ -9,6 +9,30 @@ "num": { "$id": "another-schema#num", "type": "string" + }, + "exclusiveMaximum": { + "$ref": "http://json-schema.org/draft-07/schema#/properties/exclusiveMaximum" + }, + "maxLength": { + "$ref": "http://json-schema.org/draft-07/schema#/definitions/nonNegativeInteger" + }, + "parametersList": { + "type": "array", + "description": "The parameters needed to send a valid API call.", + "additionalItems": false, + "items": [ + { + "oneOf": [ + { + "$ref": "#/definitions/parameter" + }, + { + "$ref": "#/definitions/jsonReference" + } + ] + } + ], + "uniqueItems": true } }, "properties": { @@ -20,6 +44,12 @@ "minimum": 1, "maximum": 3 }, + "exclusiveMaximum": { + "$ref": "#/definitions/exclusiveMaximum" + }, + "maxLength": { + "$ref": "#/definitions/maxLength" + }, "singleValue": {"const": "foo"}, "singleValueConstant": {"const": "foo"}, "multipleValues": {"enum": ["foo", "bar"]}, diff --git a/spec/fixtures/expected-schema-from-draft-04-to-2019.json b/spec/fixtures/expected-schema-from-draft-04-to-2019.json index 619cc6c..e626eb5 100644 --- a/spec/fixtures/expected-schema-from-draft-04-to-2019.json +++ b/spec/fixtures/expected-schema-from-draft-04-to-2019.json @@ -10,6 +10,30 @@ "$id": "another-schema", "$anchor": "num", "type": "string" + }, + "exclusiveMaximum": { + "$ref": "http://json-schema.org/draft-07/schema#/properties/exclusiveMaximum" + }, + "maxLength": { + "$ref": "http://json-schema.org/draft-07/schema#/definitions/nonNegativeInteger" + }, + "parametersList": { + "type": "array", + "description": "The parameters needed to send a valid API call.", + "additionalItems": false, + "items": [ + { + "oneOf": [ + { + "$ref": "#/definitions/parameter" + }, + { + "$ref": "#/definitions/jsonReference" + } + ] + } + ], + "uniqueItems": true } }, "properties": { @@ -21,6 +45,12 @@ "minimum": 1, "maximum": 3 }, + "exclusiveMaximum": { + "$ref": "#/definitions/exclusiveMaximum" + }, + "maxLength": { + "$ref": "#/definitions/maxLength" + }, "singleValue": {"const": "foo"}, "singleValueConstant": {"const": "foo"}, "multipleValues": {"enum": ["foo", "bar"]}, diff --git a/spec/fixtures/expected-schema-from-draft-04-to-2020.json b/spec/fixtures/expected-schema-from-draft-04-to-2020.json index ccb1222..3daac4a 100644 --- a/spec/fixtures/expected-schema-from-draft-04-to-2020.json +++ b/spec/fixtures/expected-schema-from-draft-04-to-2020.json @@ -10,6 +10,29 @@ "$id": "another-schema", "$anchor": "num", "type": "string" + }, + "exclusiveMaximum": { + "$ref": "http://json-schema.org/draft-07/schema#/properties/exclusiveMaximum" + }, + "maxLength": { + "$ref": "http://json-schema.org/draft-07/schema#/definitions/nonNegativeInteger" + }, + "parametersList": { + "type": "array", + "description": "The parameters needed to send a valid API call.", + "items": [ + { + "oneOf": [ + { + "$ref": "#/definitions/parameter" + }, + { + "$ref": "#/definitions/jsonReference" + } + ] + } + ], + "uniqueItems": true } }, "properties": { @@ -21,6 +44,12 @@ "minimum": 1, "maximum": 3 }, + "exclusiveMaximum": { + "$ref": "#/definitions/exclusiveMaximum" + }, + "maxLength": { + "$ref": "#/definitions/maxLength" + }, "singleValue": {"const": "foo"}, "singleValueConstant": {"const": "foo"}, "multipleValues": {"enum": ["foo", "bar"]}, diff --git a/spec/fixtures/schema-draft-04.json b/spec/fixtures/schema-draft-04.json index 896d480..a5d5def 100644 --- a/spec/fixtures/schema-draft-04.json +++ b/spec/fixtures/schema-draft-04.json @@ -9,6 +9,28 @@ "num": { "id": "another-schema#num", "type": "string" + }, + "exclusiveMaximum": { + "$ref": "http://json-schema.org/draft-04/schema#/properties/exclusiveMaximum" + }, + "maxLength": { + "$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveInteger" + }, + "parametersList": { + "type": "array", + "description": "The parameters needed to send a valid API call.", + "additionalItems": false, + "items": { + "oneOf": [ + { + "$ref": "#/definitions/parameter" + }, + { + "$ref": "#/definitions/jsonReference" + } + ] + }, + "uniqueItems": true } }, "properties": { @@ -24,6 +46,12 @@ "maximum": 3, "exclusiveMaximum": false }, + "exclusiveMaximum": { + "$ref": "#/definitions/exclusiveMaximum" + }, + "maxLength": { + "$ref": "#/definitions/maxLength" + }, "singleValue": {"enum": ["foo"]}, "singleValueConstant": {"constant": "foo"}, "multipleValues": {"enum": ["foo", "bar"]}, diff --git a/spec/migrate.spec.ts b/spec/migrate.spec.ts index c6fed0a..7fc5780 100644 --- a/spec/migrate.spec.ts +++ b/spec/migrate.spec.ts @@ -34,7 +34,7 @@ describe("migrate to draft-07 schema", () => { describe("invalid schemas", () => { it("should throw if id is not a string", () => { - assert.throws(() => draft7({id: 1})) + assert.throws(() => draft7({id: 1 as unknown as string})) }) it("should throw if id has many #s", () => { diff --git a/src/index.ts b/src/index.ts index 9bd444d..c496dec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -107,6 +107,22 @@ export function getAjv(): Ajv { case "exclusiveMaximum": migrateExclusive(dataSchema, key, "maximum") break + case "$ref": { + const val = dsCopy[key] + switch (val) { + case "http://json-schema.org/draft-04/schema#/properties/exclusiveMaximum": + dataSchema[key] = + "http://json-schema.org/draft-07/schema#/properties/exclusiveMaximum" + break + case "http://json-schema.org/draft-04/schema#/definitions/positiveInteger": + dataSchema[key] = + "http://json-schema.org/draft-07/schema#/definitions/nonNegativeInteger" + break + default: + dataSchema[key] = val + } + break + } case "exclusiveMinimum": migrateExclusive(dataSchema, key, "minimum") break @@ -129,16 +145,19 @@ export function getAjv(): Ajv { } break } - case "items": + case "items": { if (version === "draft2020" && Array.isArray(dsCopy.items)) { dataSchema.prefixItems = dsCopy.items if (dsCopy.additionalItems !== undefined) { dataSchema.items = dsCopy.additionalItems } + } else if (!Array.isArray(dsCopy.items) && dsCopy.items.oneOf) { + dataSchema.items = [dsCopy.items] } else { dataSchema.items = dsCopy.items } break + } case "additionalItems": if (version !== "draft2020") { dataSchema.additionalItems = dsCopy.additionalItems