-
Notifications
You must be signed in to change notification settings - Fork 1
/
not.spec.ts
46 lines (39 loc) · 1.34 KB
/
not.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* eslint-disable unicorn/no-useless-undefined */
/* eslint-disable unicorn/no-null */
import { object } from "../composite/object";
import { refineAsync } from "../refine";
import { integer, number } from "../simple/number";
import { get, into } from "../utility";
import { translate } from "../validation";
import { and } from "./and";
import { not } from "./not";
const schema = and(
not(object({ a: integer(number()) })),
object({ a: number() })
);
it("accepts", () => {
expect(schema.accepts({ a: 2.1 })).toBeTruthy();
expect(schema.accepts({ a: 2 })).toBeFalsy();
});
it("validates", () => {
expect(translate(schema.validate({ a: 2 }))).toEqual("not");
});
it("validates async", async () => {
const schema = not(
refineAsync(number(), (v, { validIf }) =>
Promise.resolve(validIf(v > 0, "must be positive"))
)
);
expect(translate(await schema.validateAsync("not a number"))).toBeUndefined();
expect(translate(await schema.validateAsync(0))).toBeUndefined();
expect(translate(await schema.validateAsync(1))).toEqual("not");
});
it("parses (by returning its input)", () => {
expect(schema.meta().schemas[0].parse({ a: 2.1 }).parsedValue).toEqual({
a: 2.1,
});
});
it("creates meta information", () => {
expect(get(schema, 0).meta().type).toEqual("not");
expect(into(get(schema, 0)).meta().type).toEqual("object");
});