This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor validator and add ajv-formats
- Loading branch information
Showing
6 changed files
with
84 additions
and
8 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
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,65 @@ | ||
import { Validator, ValidationSchema, ValidationError } from "./validator" | ||
|
||
test("validate() validates objects", () => { | ||
const someObjValidator = new Validator<SomeObj>("someObj", someObjSchema) | ||
for (const valid of validSomeObjs) { | ||
expect(someObjValidator.validate(valid)).toBe(true) | ||
} | ||
for (const invalid of invalidSomeObjs) { | ||
expect(someObjValidator.validate(invalid)).toBe(false) | ||
} | ||
}) | ||
|
||
test("assert() validates objects and throws if the validation fails", () => { | ||
const someObjValidator = new Validator<SomeObj>("someObj", someObjSchema) | ||
for (const valid of validSomeObjs) { | ||
expect(() => Validator.assert(someObjValidator, valid)).not.toThrow() | ||
} | ||
for (const invalid of invalidSomeObjs) { | ||
expect(() => Validator.assert(someObjValidator, invalid)).toThrow( | ||
ValidationError | ||
) | ||
} | ||
}) | ||
|
||
test("Supports additional ajv-formats like date", () => { | ||
const validator = new Validator("date", { | ||
type: "string", | ||
format: "date", | ||
formatMinimum: "2016-02-06", | ||
formatExclusiveMaximum: "2016-12-27", | ||
}) | ||
|
||
const validData = ["2016-02-06", "2016-12-26"] | ||
for (const valid of validData) { | ||
expect(validator.validate(valid)).toBe(true) | ||
} | ||
|
||
const invalidData = ["2016-02-05", "2016-12-27", "abc"] | ||
for (const invalid of invalidData) { | ||
expect(validator.validate(invalid)).toBe(false) | ||
} | ||
}) | ||
|
||
interface SomeObj { | ||
n: number | ||
s: string[] | ||
o?: { m: number } | ||
} | ||
|
||
const someObjSchema: ValidationSchema = { | ||
type: "object", | ||
properties: { | ||
n: { type: "number" }, | ||
s: { type: "array", items: { type: "string" } }, | ||
o: { type: "object", properties: { m: { type: "number" } } }, | ||
}, | ||
required: ["n", "s"], | ||
} | ||
|
||
const validSomeObjs: SomeObj[] = [ | ||
{ n: 1, s: ["s1"] }, | ||
{ n: 1, s: ["s1"], o: { m: 1 } }, | ||
] | ||
|
||
const invalidSomeObjs = [{}, { n: "1", s: ["s1"] }] |
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