Debian Codegraph @

spec/options/options_validation.spec.ts

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import type Ajv from "../.."
2 import _Ajv from "../ajv"
3 import chai from "../chai"
4 chai.should()
5
6 const DATE_FORMAT = /^\d\d\d\d-[0-1]\d-[0-3]\d$/
7
8 describe("validation options", () => {
9 describe("format", () => {
10 it("should not validate formats if option format == false", () => {
11 const ajv = new _Ajv({formats: {date: DATE_FORMAT}}),
12 ajvFF = new _Ajv({formats: {date: DATE_FORMAT}, validateFormats: false})
13
14 const schema = {type: "string", format: "date"}
15 const invalideDateTime = "06/19/1963" // expects hyphens
16
17 ajv.validate(schema, invalideDateTime).should.equal(false)
18 ajvFF.validate(schema, invalideDateTime).should.equal(true)
19 })
20 })
21
22 describe("formats", () => {
23 it("should add formats from options", () => {
24 const ajv = new _Ajv({
25 allowUnionTypes: true,
26 formats: {
27 identifier: /^[a-z_$][a-z0-9_$]*$/i,
28 },
29 })
30
31 const validate = ajv.compile({
32 type: ["string", "number"],
33 format: "identifier",
34 })
35
36 validate("Abc1").should.equal(true)
37 validate("foo bar").should.equal(false)
38 validate("123").should.equal(false)
39 validate(123).should.equal(true)
40 })
41 })
42
43 describe("keywords", () => {
44 it("should add keywords from options", () => {
45 const ajv = new _Ajv({
46 allowUnionTypes: true,
47 keywords: [
48 {
49 keyword: "identifier",
50 type: "string",
51 validate: function (_schema, data) {
52 return /^[a-z_$][a-z0-9_$]*$/i.test(data)
53 },
54 },
55 ],
56 })
57
58 testKeyword(ajv)
59 })
60
61 it("should support old keywords option as map", () => {
62 const ajv = new _Ajv({
63 allowUnionTypes: true,
64 keywords: {
65 //@ts-expect-error
66 identifier: {
67 type: "string",
68 schema: false,
69 validate: function (data: string) {
70 return /^[a-z_$][a-z0-9_$]*$/i.test(data)
71 },
72 },
73 },
74 logger: false,
75 })
76
77 testKeyword(ajv)
78 })
79
80 function testKeyword(ajv: Ajv) {
81 const validate = ajv.compile({
82 type: ["string", "number"],
83 identifier: true,
84 })
85
86 validate("Abc1").should.equal(true)
87 validate("foo bar").should.equal(false)
88 validate("123").should.equal(false)
89 validate(123).should.equal(true)
90 }
91 })
92
93 describe("unicode", () => {
94 it("should use String.prototype.length with deprecated unicode option == false", () => {
95 const ajvUnicode = new _Ajv()
96 testUnicode(new _Ajv({unicode: false, logger: false}))
97 testUnicode(new _Ajv({unicode: false, allErrors: true, logger: false}))
98
99 function testUnicode(ajv: Ajv) {
100 let validateWithUnicode = ajvUnicode.compile({type: "string", minLength: 2})
101 let validate = ajv.compile({type: "string", minLength: 2})
102
103 validateWithUnicode("😀").should.equal(false)
104 validate("😀").should.equal(true)
105
106 validateWithUnicode = ajvUnicode.compile({type: "string", maxLength: 1})
107 validate = ajv.compile({type: "string", maxLength: 1})
108
109 validateWithUnicode("😀").should.equal(true)
110 validate("😀").should.equal(false)
111 }
112 })
113 })
114
115 describe("multipleOfPrecision", () => {
116 it("should allow for some deviation from 0 when validating multipleOf with value < 1", () => {
117 test(new _Ajv({multipleOfPrecision: 7}))
118 test(new _Ajv({multipleOfPrecision: 7, allErrors: true}))
119
120 function test(ajv) {
121 let schema = {type: "number", multipleOf: 0.01}
122 let validate = ajv.compile(schema)
123
124 validate(4.18).should.equal(true)
125 validate(4.181).should.equal(false)
126
127 schema = {type: "number", multipleOf: 0.0000001}
128 validate = ajv.compile(schema)
129
130 validate(53.198098).should.equal(true)
131 validate(53.1980981).should.equal(true)
132 validate(53.19809811).should.equal(false)
133 }
134 })
135 })
136 })