Debian Codegraph @

ajv-formats/tests/formatLimit.spec.ts

raw symbols SCIP
1 import Ajv from "ajv"
2 import {fullFormats, fastFormats} from "../dist/formats"
3 import formatLimit from "../dist/limit"
4 import ajvFormats from "../dist"
5
6 describe('keywords "formatMinimum" and "formatMaximum"', () => {
7 const ajvs = getAjvs(true)
8 const ajvsFF = getAjvs(false)
9
10 ajvs.forEach((ajv, i) => {
11 it(`should not validate formatMaximum/Minimum if option format == false #${i}`, () => {
12 const ajvFF = ajvsFF[i]
13
14 const schema = {
15 type: "string",
16 format: "date",
17 formatMaximum: "2015-08-01",
18 }
19
20 const date = "2015-09-01"
21 expect(ajv.validate(schema, date)).toEqual(false)
22 expect(ajvFF.validate(schema, date)).toEqual(true)
23 })
24 })
25
26 ajvs.forEach((ajv, i) => {
27 it(`should throw when "format" is absent #${i}`, () => {
28 expect(() => ajv.compile({type: "string", formatMaximum: "2015-08-01"})).toThrow()
29 })
30 })
31
32 ajvs.forEach((ajv, i) => {
33 it(`formatExclusiveMaximum should throw if not boolean #${i}`, () => {
34 expect(() =>
35 ajv.compile({
36 type: "string",
37 formatMaximum: "2015-08-01",
38 formatExclusiveMaximum: 1,
39 })
40 ).toThrow()
41 })
42 })
43
44 ajvs.forEach((ajv, i) => {
45 it(`formatExclusiveMaximum should throw when "formatMaximum" is absent #${i}`, () => {
46 expect(() => ajv.compile({type: "string", formatExclusiveMaximum: true})).toThrow()
47 })
48 })
49
50 function getAjvs(validateFormats: boolean): Ajv[] {
51 return [
52 formatLimit(new Ajv({allErrors: true, validateFormats, formats: fullFormats})),
53 formatLimit(new Ajv({allErrors: true, validateFormats, formats: fastFormats})),
54 ajvFormats(new Ajv({allErrors: true, validateFormats})),
55 ajvFormats(new Ajv({validateFormats}), {keywords: true}),
56 ]
57 }
58 })