-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: issue#58 merge missing parent properties in oneOf-object resolution
- Loading branch information
Showing
2 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { strict as assert } from "assert"; | ||
import { Draft07 as Draft } from "../../../lib/draft07"; | ||
|
||
describe("issue#58 - oneOf should invalid type error", () => { | ||
let draft: Draft; | ||
beforeEach(() => (draft = new Draft())); | ||
|
||
it("should return one-of-error for invalid type", () => { | ||
draft.setSchema({ | ||
oneOf: [{ type: "null" }, { type: "number" }] | ||
}); | ||
const errors = draft.validate("string"); | ||
assert(errors.length > 0); | ||
}); | ||
|
||
it("should validate correct type defined in one-of statement", () => { | ||
draft.setSchema({ | ||
oneOf: [{ type: "null" }, { type: "number" }] | ||
}); | ||
const errors = draft.validate(123); | ||
assert(errors.length === 0); | ||
}); | ||
|
||
it("should return type-error for non-integer value", () => { | ||
draft.setSchema({ | ||
properties: { | ||
foo: { | ||
properties: { | ||
number: { | ||
type: "integer" | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const errors = draft.validate({ foo: { number: "not an integer" } }); | ||
assert(errors.length > 0); | ||
}); | ||
|
||
it("should return type-error for non-integer value in combination with oneOf", () => { | ||
draft.setSchema({ | ||
properties: { | ||
foo: { | ||
properties: { | ||
number: { | ||
type: "integer" | ||
} | ||
}, | ||
oneOf: [ | ||
{ | ||
type: "null" | ||
}, | ||
{ | ||
type: "object" | ||
} | ||
] | ||
} | ||
} | ||
}); | ||
const errors = draft.validate({ foo: { number: "not an integer" } }); | ||
assert(errors.length > 0); | ||
}); | ||
|
||
it("should return type-error", () => { | ||
const schema = { | ||
$schema: "http://json-schema.org/draft-04/schema#", | ||
properties: { | ||
foo: { | ||
additionalProperties: false, | ||
properties: { | ||
number: { | ||
type: "integer" | ||
} | ||
}, | ||
oneOf: [ | ||
{ | ||
type: "null" | ||
}, | ||
{ | ||
type: "object" | ||
} | ||
] | ||
} | ||
}, | ||
additionalProperties: false, | ||
oneOf: [ | ||
{ | ||
type: "null" | ||
}, | ||
{ | ||
type: "object" | ||
} | ||
] | ||
}; | ||
const inputData: { | ||
foo: { number: string }; | ||
} = { | ||
foo: { | ||
number: "not an integer" | ||
} | ||
}; | ||
|
||
draft.setSchema(schema); | ||
const errors = draft.validate(inputData); | ||
assert.equal(errors.length, 1); | ||
assert.equal(errors[0].code, "type-error"); | ||
}); | ||
|
||
it("should validate without type-error", () => { | ||
const schema = { | ||
$schema: "http://json-schema.org/draft-04/schema#", | ||
properties: { | ||
foo: { | ||
additionalProperties: false, | ||
properties: { | ||
number: { | ||
type: "integer" | ||
} | ||
}, | ||
oneOf: [ | ||
{ | ||
type: "null" | ||
}, | ||
{ | ||
type: "object" | ||
} | ||
] | ||
} | ||
}, | ||
additionalProperties: false, | ||
oneOf: [ | ||
{ | ||
type: "null" | ||
}, | ||
{ | ||
type: "object" | ||
} | ||
] | ||
}; | ||
const inputData = { | ||
foo: { | ||
number: 123 | ||
} | ||
}; | ||
|
||
draft.setSchema(schema); | ||
const errors = draft.validate(inputData); | ||
assert.equal(errors.length, 0); | ||
}); | ||
}); |