/
examples-typescript.ts
206 lines (183 loc) · 5.09 KB
/
examples-typescript.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// make sure tsc can build this
import { expect } from 'chai'
import {
IValueOrError,
IBlueprint,
IValidationContext,
Validator,
blueprint,
registerValidator,
registerType,
registerBlueprint,
registerExpression,
optional,
required,
gt, gte, lt, lte, range,
is
} from '@polyn/blueprint'
;(() => {
expect(is.string('str'), `is.string('str') should work`).to.equal(true)
expect(is.not.string(1), `is.not.string(1) should work`).to.equal(true)
expect(is.func(() => true), `is.func(() => true should work`).to.equal(true)
expect(is.not.func('str'), `is.not.func('str') should work`).to.equal(true)
})()
;(() => {
const isCharBool: Validator = (context: IValidationContext): boolean => {
return typeof context.value === 'string' && context.value.length === 1
}
const isCharVal: Validator = (context: IValidationContext): IValueOrError => {
if (typeof context.value === 'string' && context.value.length === 1) {
return { value: context.value }
}
return {
err: new Error('BOOM!'),
messages: ['BOOM!', 'BOOM AGAIN!'],
value: context.value
}
}
registerValidator('oneChar', isCharBool),
registerValidator('oneChar2', isCharVal),
registerType('char', isCharBool),
registerType('char2', isCharVal)
registerExpression('productType', /^book|movie$/)
const myCharsBp: IBlueprint = registerBlueprint('myChars', {
a: 'oneChar',
b: 'oneChar2',
c: 'char',
d: 'char2'
})
expect(myCharsBp.name).to.equal('myChars')
const myModelBp: IBlueprint = blueprint('myModel', {
prop1: 'string',
prop2: 'number',
prop3: 'myChars',
prop4: 'productType',
prop5: 'oneChar',
prop6: 'oneChar2',
prop7: 'char',
prop8: 'char2',
gt: gt(20),
gte: gte(20),
lt: lt(20),
lte: lte(20),
range: range({ gt: 10, lt: 20 }),
maybeGt: optional.gt(20),
maybeGte: optional.gte(20),
maybeLt: optional.lt(20),
maybeLte: optional.lte(20),
maybeRange: optional.range({ gt: 10, lt: 20 })
})
expect(myModelBp.name).to.equal('myModel')
const result: IValueOrError = myModelBp.validate({
prop1: 'hello world!',
prop2: 42,
prop3: {
a: 'a',
b: 'b',
c: 'c',
d: 'd'
},
prop4: 'book',
prop5: 'a',
prop6: 'b',
prop7: 'c',
prop8: 'd',
gt: 21,
gte: 21,
lt: 19,
lte: 19,
range: 15,
maybeGt: null,
maybeGte: undefined,
maybeLt: null,
maybeLte: undefined,
maybeRange: null
})
expect(result.err).to.be.null
})()
;(() => {
// Exploring `optional`
const optionalValues = blueprint('optionalValues', {
stringOrDefault: optional('string').withDefault('foo'),
maybeGt20: optional(gt(20)),
gt20OrDefault: optional(gt(20)).withDefault(42),
maybeEnum: optional(/^book|magazine$/),
enumOrDefault: optional(/^book|magazine|product$/).withDefault('product'),
maybeCustom: optional(({ value }) => typeof value === 'string')
.withDefault('custom'),
fromContext: optional('string')
.from(({ input }: IValidationContext) => input.context.string),
fromContextWithDefault: (optional('string')
.from(({ input }: IValidationContext) => input.context.string2) as optional)
.withDefault('default!')
})
const result = optionalValues.validate({
context: {
string: 'foo'
}
});
expect(result.err).to.be.null
expect(result.value).to.deep.equal({
stringOrDefault: 'foo',
maybeGt20: undefined,
gt20OrDefault: 42,
maybeEnum: undefined,
enumOrDefault: 'product',
maybeCustom: 'custom',
fromContext: 'foo',
fromContextWithDefault: 'default!'
})
})()
;(() => {
// Exploring `required`
const optionalValues = blueprint('optionalValues', {
requiredString: required('string')
.from(({ input }: IValidationContext) => input.context.string),
requiredGt: required(gt(10))
.from(({ input }: IValidationContext) => input.context.twelve),
requiredExp: required(/^book|magazine$/)
.from(({ input }: IValidationContext) => input.context.type)
})
const result = optionalValues.validate({
context: {
string: 'foo',
twelve: 12,
type: 'book'
}
});
expect(result.err).to.be.null
expect(result.value).to.deep.equal({
requiredString: 'foo',
requiredGt: 12,
requiredExp: 'book'
})
})()
;(() => {
// Strictly Typed IValueOrError
interface IAmValidationType {
prop1: string;
prop2: number;
}
interface IHaveValidationType {
validationType: IAmValidationType;
}
const bp: IBlueprint<IHaveValidationType> = blueprint('typedValidation', {
validationType: (
{ value }: IValidationContext<IAmValidationType>
): IValueOrError<IAmValidationType> => {
if (value && is.string(value.prop1) && is.number(value.prop2)) {
return { value };
}
return { err: new Error('Boom!') };
}
});
const expected = {
validationType: {
prop1: 'hello',
prop2: 42
}
};
expect(bp.validate(expected).value).to.deep.equal(expected);
expect(bp.validate({}).err).to.not.be.null;
})()
console.log('\x1b[32mALL TYPESCRIPT EXAMPLES PASSED\x1b[0m\n');