Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minor issues while converting draft4 to draft7 #1

Merged
merged 3 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions spec/fixtures/expected-schema-from-draft-04-to-07.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"]},
Expand Down
30 changes: 30 additions & 0 deletions spec/fixtures/expected-schema-from-draft-04-to-2019.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"]},
Expand Down
29 changes: 29 additions & 0 deletions spec/fixtures/expected-schema-from-draft-04-to-2020.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"]},
Expand Down
28 changes: 28 additions & 0 deletions spec/fixtures/schema-draft-04.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"]},
Expand Down
2 changes: 1 addition & 1 deletion spec/migrate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] = dsCopy[key]
mvhysko marked this conversation as resolved.
Show resolved Hide resolved
}
break
}
case "exclusiveMinimum":
migrateExclusive(dataSchema, key, "minimum")
break
Expand All @@ -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
Expand Down