@digitalsamba/validate 2.0.0-canary.1b923c9db3e2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,124 @@
1
+ import * as T from '../lib/validation'
2
+
3
+ describe('validations', () => {
4
+ it('Returns referentially identical objects', () => {
5
+ const validator = T.object({
6
+ name: T.string,
7
+ items: T.arrayOf(T.string.nullable()),
8
+ })
9
+
10
+ const value = {
11
+ name: 'toad',
12
+ items: ['toad', 'berd', null, 'bot'],
13
+ }
14
+
15
+ expect(validator.validate(value)).toStrictEqual(value)
16
+ })
17
+ it('Rejects unknown object keys', () => {
18
+ expect(() =>
19
+ T.object({ moo: T.literal('cow') }).validate({ moo: 'cow', cow: 'moo' })
20
+ ).toThrowErrorMatchingInlineSnapshot(`"At cow: Unexpected property"`)
21
+ })
22
+ it('Produces nice error messages', () => {
23
+ expect(() =>
24
+ T.object({
25
+ toad: T.object({
26
+ name: T.number,
27
+ friends: T.arrayOf(
28
+ T.object({
29
+ name: T.string,
30
+ })
31
+ ),
32
+ }),
33
+ }).validate({
34
+ toad: {
35
+ name: 'toad',
36
+ friends: [
37
+ {
38
+ name: 'bird',
39
+ },
40
+
41
+ {
42
+ name: 1235,
43
+ },
44
+ ],
45
+ },
46
+ })
47
+ ).toThrowErrorMatchingInlineSnapshot(`"At toad.name: Expected number, got a string"`)
48
+
49
+ expect(() =>
50
+ T.model(
51
+ 'shape',
52
+ T.object({
53
+ id: T.string,
54
+ x: T.number,
55
+ y: T.number,
56
+ })
57
+ ).validate({
58
+ id: 'abc123',
59
+ x: 132,
60
+ y: NaN,
61
+ })
62
+ ).toThrowErrorMatchingInlineSnapshot(`"At shape(id = abc123).y: Expected a number, got NaN"`)
63
+
64
+ expect(() =>
65
+ T.model(
66
+ 'shape',
67
+ T.object({
68
+ id: T.string,
69
+ color: T.setEnum(new Set(['red', 'green', 'blue'])),
70
+ })
71
+ ).validate({ id: 'abc13', color: 'rubbish' })
72
+ ).toThrowErrorMatchingInlineSnapshot(
73
+ `"At shape(id = abc13).color: Expected \\"red\\" or \\"green\\" or \\"blue\\", got rubbish"`
74
+ )
75
+ })
76
+
77
+ it('Understands unions & produces nice error messages', () => {
78
+ const catSchema = T.object({
79
+ type: T.literal('cat'),
80
+ id: T.string,
81
+ meow: T.boolean,
82
+ })
83
+ const dogSchema = T.object({
84
+ type: T.literal('dog'),
85
+ id: T.string,
86
+ bark: T.boolean,
87
+ })
88
+ const animalSchema = T.union('type', {
89
+ cat: catSchema,
90
+ dog: dogSchema,
91
+ })
92
+
93
+ const nested = T.object({
94
+ animal: animalSchema,
95
+ })
96
+
97
+ expect(() =>
98
+ nested.validate({ animal: { type: 'cow', moo: true, id: 'abc123' } })
99
+ ).toThrowErrorMatchingInlineSnapshot(
100
+ `"At animal.type: Expected one of \\"cat\\" or \\"dog\\", got \\"cow\\""`
101
+ )
102
+
103
+ expect(() =>
104
+ nested.validate({ animal: { type: 'cat', meow: 'yes', id: 'abc123' } })
105
+ ).toThrowErrorMatchingInlineSnapshot(
106
+ `"At animal(type = cat).meow: Expected boolean, got a string"`
107
+ )
108
+
109
+ expect(() =>
110
+ T.model('animal', animalSchema).validate({ type: 'cat', moo: true, id: 'abc123' })
111
+ ).toThrowErrorMatchingInlineSnapshot(
112
+ `"At animal(id = abc123, type = cat).meow: Expected boolean, got undefined"`
113
+ )
114
+ })
115
+ })
116
+
117
+ describe('T.refine', () => {
118
+ it.todo('Refines a validator.')
119
+ it.todo('Produces a type error if the refinement is not of the correct type.')
120
+ })
121
+
122
+ describe('T.check', () => {
123
+ it.todo('Adds a check to a validator.')
124
+ })