Debian Codegraph @

spec/types/async-validate.spec.ts

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 import type {AnySchemaObject, SchemaObject, AsyncSchema} from "../.."
2 import _Ajv from "../ajv"
3 import chai from "../chai"
4 const should = chai.should()
5
6 interface Foo {
7 foo: number
8 }
9
10 describe("$async validation and type guards", () => {
11 const ajv = new _Ajv({strictTypes: false})
12
13 describe("$async: undefined", () => {
14 it("should have result type boolean 1", () => {
15 const validate = ajv.compile<Foo>({
16 type: "object",
17 properties: {foo: {type: "number"}},
18 })
19 const data: unknown = {foo: 1}
20 let result: boolean
21 if ((result = validate(data))) {
•22 // @ts-ignore
23 data.foo.should.equal(1)
24 }
•25 // @ts-ignore
26 result.should.equal(true)
27 })
28
29 it("should have result type boolean 2", () => {
30 const schema: SchemaObject = {
31 type: "object",
32 properties: {foo: {type: "number"}},
33 }
34 const validate = ajv.compile<Foo>(schema)
35 const data: unknown = {foo: 1}
36 let result: boolean
37 if ((result = validate(data))) {
•38 // @ts-ignore
39 data.foo.should.equal(1)
40 }
•41 // @ts-ignore
42 result.should.equal(true)
43 })
44
45 it("should have result type boolean 3", () => {
46 const schema: AnySchemaObject = {
47 type: "object",
48 properties: {foo: {type: "number"}},
49 }
50 const validate = ajv.compile<Foo>(schema)
51 const data: unknown = {foo: 1}
52 let result: boolean
53 if ((result = validate(data))) {
•54 // @ts-ignore
55 data.foo.should.equal(1)
56 }
•57 // @ts-ignore
58 result.should.equal(true)
59 })
60 })
61
62 describe("$async: false", () => {
63 it("should have result type boolean 1", () => {
64 const validate = ajv.compile({$async: false})
65 const result: boolean = validate({})
66 should.exist(result)
67 })
68
69 it("should have result type boolean 2", () => {
70 const schema: SchemaObject = {$async: false}
71 const validate = ajv.compile(schema)
72 const result: boolean = validate({})
73 should.exist(result)
74 })
75
76 it("should have result type boolean 3", () => {
77 const schema: AnySchemaObject = {$async: false}
78 const validate = ajv.compile(schema)
79 const result: boolean = validate({})
80 should.exist(result)
81 })
82 })
83
84 describe("$async: true", () => {
85 it("should have result type promise 1", async () => {
86 const validate = ajv.compile<Foo>({
87 $async: true,
88 type: "object",
89 properties: {foo: {type: "number"}},
90 })
91 const result: Promise<Foo> = validate({foo: 1})
•92 // @ts-ignore
93 await result.then((data) => data.should.exist)
94 })
95
96 it("should have result type promise 2", async () => {
97 const schema: AsyncSchema = {
98 $async: true,
99 type: "object",
100 properties: {foo: {type: "number"}},
101 }
102 const validate = ajv.compile<Foo>(schema)
103 const result: Promise<Foo> = validate({foo: 1})
•104 // @ts-ignore
105 await result.then((data) => data.foo.should.equal(1))
106 })
107 })
108
109 describe("$async: boolean", () => {
110 it("should have result type boolean | promise 1", async () => {
111 const schema = {
112 $async: true,
113 type: "object",
114 properties: {foo: {type: "number"}},
115 }
116 const validate = ajv.compile<Foo>(schema)
117 const data: unknown = {foo: 1}
118 let result: boolean | Promise<Foo>
119 if ((result = validate(data))) {
120 if (result instanceof Promise) {
•121 // @ts-ignore
122 await result.then((_data) => _data.foo.should.equal(1))
123 } else {
124 should.fail()
125 }
126 } else {
127 should.fail()
128 }
129 })
130
131 it("should have result type boolean | promise 2", async () => {
132 const schema = {$async: false}
133 const validate = ajv.compile<any>(schema)
134 const result = validate({})
135 if (typeof result === "boolean") {
136 should.exist(result)
137 } else {
•138 // @ts-ignore
139 await result.then((data) => data.should.exist)
140 }
141 })
142 })
143
144 describe("$async: unknown", () => {
145 const schema: Record<string, unknown> = {
146 type: "object",
147 properties: {foo: {type: "number"}},
148 }
149 const validate = ajv.compile<Foo>(schema)
150
151 it("should have result type boolean", () => {
152 const data = {foo: 1}
153 let result: boolean
154 if ((result = validate(data))) {
•155 // @ts-ignore
156 data.foo.should.equal(1)
157 }
•158 // @ts-ignore
159 result.should.equal(true)
160 })
161 })
162
163 describe("schema: any", () => {
164 const schema: any = {}
165 const validate = ajv.compile(schema)
166 it("should have result type boolean | promise", () => {
167 const result: boolean = validate({})
•168 // @ts-ignore
169 result.should.equal(true)
170 })
171 })
172 })