Debian Codegraph @

ajv-formats/tests/index.spec.ts

raw symbols SCIP
1 import Ajv, {FormatDefinition} from "ajv"
2 import addFormats from "../dist"
3 import type {FormatName} from "../dist"
4
5 describe("addFormats options", () => {
6 let ajv: Ajv
7
8 beforeEach(() => {
9 ajv = new Ajv({strictTypes: false})
10 })
11
12 test("should add passed list of formats", () => {
13 addFormats(ajv, ["date", "time"])
14 const validateDate = ajv.compile({format: "date"})
15 expect(validateDate("2020-09-17")).toEqual(true)
16 expect(validateDate("2020-09-35")).toEqual(false)
17
18 const validateTime = ajv.compile({format: "time"})
19 expect(validateTime("17:27:38Z")).toEqual(true)
20 expect(validateDate("25:27:38Z")).toEqual(false)
21
22 expect(() => ajv.compile({format: "date-time"})).toThrow()
23 addFormats(ajv, ["date-time"])
24 expect(() => ajv.compile({format: "date-time"})).not.toThrow()
25 })
26
27 test("should support validation mode", () => {
28 addFormats(ajv, {mode: "fast", formats: ["date", "time"]})
29 const validateDate = ajv.compile({format: "date"})
30 expect(validateDate("2020-09-17")).toEqual(true)
31 expect(validateDate("2020-09-35")).toEqual(true)
32 expect(validateDate("2020-09")).toEqual(false)
33
34 const validateTime = ajv.compile({format: "time"})
35 expect(validateTime("17:27:38Z")).toEqual(true)
36 expect(validateTime("25:27:38Z")).toEqual(true)
37 expect(validateTime("17:27")).toEqual(false)
38 })
39 })
40
41 describe("method get", () => {
42 test("should return format definition", () => {
43 const timeFormat = addFormats.get("time")
44 expect((timeFormat as FormatDefinition<string>).validate).toBeInstanceOf(Object)
45
46 const fastTimeFormat = addFormats.get("time", "fast")
47 expect((fastTimeFormat as FormatDefinition<string>).validate).toBeInstanceOf(RegExp)
48
49 expect(() => addFormats.get("unknown" as FormatName)).toThrow()
50 })
51 })