@based/schema 2.0.0 → 2.1.0

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.
package/test/text.ts DELETED
@@ -1,348 +0,0 @@
1
- import { setWalker, BasedSchema } from '../src'
2
- import test from 'ava'
3
- import { resultCollect } from './utils'
4
- import { languages } from '../src/languages'
5
-
6
- const schema: BasedSchema = {
7
- types: {
8
- thing: {
9
- prefix: 'ti',
10
- fields: {
11
- something: { type: 'string', format: 'strongPassword' },
12
- },
13
- },
14
- bla: {
15
- prefix: 'bl',
16
- fields: {
17
- text: {
18
- type: 'text',
19
- pattern: '[^xz]{1,10}',
20
- },
21
- },
22
- },
23
- },
24
- $defs: {},
25
- language: 'en',
26
- translations: ['de', 'nl', 'ro', 'za', 'ae'],
27
- languageFallbacks: {
28
- en: ['en'],
29
- de: ['en'],
30
- fr: ['aa'],
31
- },
32
- root: {
33
- fields: {},
34
- },
35
- prefixToTypeMapping: {
36
- bl: 'bla',
37
- ti: 'thing',
38
- },
39
- }
40
-
41
- let r
42
-
43
- test('throw error no language', async (t) => {
44
- let r = await setWalker(schema, {
45
- $id: 'bl120',
46
- text: { $value: 'x' },
47
- })
48
- t.true(r.errors.length > 0)
49
- })
50
-
51
- test('simple case', async (t) => {
52
- let r = await setWalker(schema, {
53
- $id: 'bl120',
54
- text: { en: 'flap' },
55
- })
56
- t.deepEqual(resultCollect(r), [
57
- { path: ['text', 'en'], value: 'flap' },
58
- { path: ['text'], value: { en: 'flap' } },
59
- ])
60
- })
61
-
62
- test('simple case $value', async (t) => {
63
- let r = await setWalker(schema, {
64
- $id: 'bl120',
65
- text: { en: { $value: 'flap' } },
66
- })
67
- t.deepEqual(resultCollect(r), [
68
- { path: ['text', 'en'], value: 'flap' },
69
- { path: ['text'], value: { en: { $value: 'flap' } } },
70
- ])
71
- })
72
-
73
- test('simple case $language', async (t) => {
74
- let r = await setWalker(schema, {
75
- $id: 'bl120',
76
- $language: 'en',
77
- text: 'flap',
78
- })
79
- t.deepEqual(resultCollect(r), [
80
- { path: ['text', 'en'], value: 'flap' },
81
- { path: ['text'], value: { en: 'flap' } },
82
- ])
83
- })
84
-
85
- test('simple case with value', async (t) => {
86
- r = await setWalker(schema, {
87
- $id: 'bl120',
88
- $language: 'za',
89
- text: { $value: 'flap' },
90
- })
91
- t.deepEqual(resultCollect(r), [
92
- { path: ['text', 'za'], value: 'flap' },
93
- { path: ['text'], value: { $value: 'flap' } },
94
- ])
95
- })
96
-
97
- test('simple case $value /w obj', async (t) => {
98
- let r = await setWalker(schema, {
99
- $id: 'bl120',
100
- text: { $value: { en: 'flap' } },
101
- })
102
- t.deepEqual(resultCollect(r), [
103
- { path: ['text', 'en'], value: 'flap' },
104
- { path: ['text'], value: { $value: { en: 'flap' } } },
105
- ])
106
- })
107
-
108
- test('text default', async (t) => {
109
- r = await setWalker(schema, {
110
- $id: 'bl120',
111
- $language: 'za',
112
- text: { $default: 'sdsdds' },
113
- })
114
- t.deepEqual(resultCollect(r), [
115
- { path: ['text', 'za'], value: { $default: 'sdsdds' } },
116
- { path: ['text'], value: { $default: 'sdsdds' } },
117
- ])
118
- })
119
-
120
- test('default and lang:default', async (t) => {
121
- r = await setWalker(schema, {
122
- $id: 'bl120',
123
- $language: 'za',
124
- text: { $default: 'sdsdds', en: { $default: 'flapflap' } },
125
- })
126
-
127
- t.deepEqual(resultCollect(r), [
128
- { path: ['text', 'za'], value: { $default: 'sdsdds' } },
129
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
130
- {
131
- path: ['text'],
132
- value: { $default: 'sdsdds', en: { $default: 'flapflap' } },
133
- },
134
- ])
135
- })
136
-
137
- test('default: lang, lang', async (t) => {
138
- r = await setWalker(schema, {
139
- $id: 'bl120',
140
- $language: 'za',
141
- text: { $default: { de: 'dsnfds' }, en: { $default: 'flapflap' } },
142
- })
143
- t.deepEqual(resultCollect(r), [
144
- { path: ['text', 'de'], value: { $default: 'dsnfds' } },
145
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
146
- {
147
- path: ['text'],
148
- value: { $default: { de: 'dsnfds' }, en: { $default: 'flapflap' } },
149
- },
150
- ])
151
- })
152
-
153
- test('defaullt:lang, lang, lang:default', async (t) => {
154
- r = await setWalker(schema, {
155
- $id: 'bl120',
156
- $language: 'za',
157
- text: {
158
- $default: { de: 'dsnfds' },
159
- nl: 'flapperonus',
160
- en: { $default: 'flapflap' },
161
- },
162
- })
163
- t.deepEqual(resultCollect(r), [
164
- { path: ['text', 'de'], value: { $default: 'dsnfds' } },
165
- { path: ['text', 'nl'], value: 'flapperonus' },
166
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
167
- {
168
- path: ['text'],
169
- value: {
170
- $default: { de: 'dsnfds' },
171
- nl: 'flapperonus',
172
- en: { $default: 'flapflap' },
173
- },
174
- },
175
- ])
176
- })
177
-
178
- test('default:lang, lang, lang:value, lang:default', async (t) => {
179
- r = await setWalker(schema, {
180
- $id: 'bl120',
181
- $language: 'za',
182
- text: {
183
- $default: { de: 'dsnfds' },
184
- nl: 'flapperonus',
185
- ro: { $value: 'durp' },
186
- en: { $default: 'flapflap' },
187
- },
188
- })
189
-
190
- t.deepEqual(resultCollect(r), [
191
- { path: ['text', 'de'], value: { $default: 'dsnfds' } },
192
- { path: ['text', 'nl'], value: 'flapperonus' },
193
- { path: ['text', 'ro'], value: 'durp' },
194
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
195
- {
196
- path: ['text'],
197
- value: {
198
- $default: { de: 'dsnfds' },
199
- nl: 'flapperonus',
200
- ro: { $value: 'durp' },
201
- en: { $default: 'flapflap' },
202
- },
203
- },
204
- ])
205
- })
206
-
207
- test('value:lang, lang, default:lang, lang:value, lang:default', async (t) => {
208
- r = await setWalker(schema, {
209
- $id: 'bl120',
210
- $language: 'za',
211
- text: {
212
- $value: 'durp',
213
- nl: 'flapperonus',
214
- $default: {
215
- ae: 'habibi',
216
- },
217
- ro: { $value: 'durp' },
218
- en: { $default: 'flapflap' },
219
- },
220
- })
221
-
222
- t.deepEqual(resultCollect(r), [
223
- { path: ['text', 'za'], value: 'durp' },
224
- { path: ['text', 'nl'], value: 'flapperonus' },
225
- { path: ['text', 'ae'], value: { $default: 'habibi' } },
226
- { path: ['text', 'ro'], value: 'durp' },
227
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
228
- {
229
- path: ['text'],
230
- value: {
231
- $value: 'durp',
232
- nl: 'flapperonus',
233
- $default: {
234
- ae: 'habibi',
235
- },
236
- ro: { $value: 'durp' },
237
- en: { $default: 'flapflap' },
238
- },
239
- },
240
- ])
241
- })
242
-
243
- test('value: wrong pattern, lang, default:lang, lang:value, lang:default', async (t) => {
244
- r = await setWalker(schema, {
245
- $id: 'bl120',
246
- $language: 'za',
247
- text: {
248
- $value: 'xz',
249
- nl: 'flapperonus',
250
- $default: {
251
- ae: 'habibi',
252
- },
253
- ro: { $value: 'durp' },
254
- en: { $default: 'xzxz' },
255
- },
256
- })
257
- t.true(r.errors.length > 0)
258
- })
259
-
260
- test('text delete', async (t) => {
261
- r = await setWalker(schema, {
262
- $id: 'bl120',
263
- text: {
264
- $delete: true,
265
- },
266
- })
267
- t.deepEqual(resultCollect(r), [{ path: ['text'], value: { $delete: true } }])
268
- })
269
-
270
- test('text delete single language', async (t) => {
271
- r = await setWalker(schema, {
272
- $id: 'bl120',
273
- text: {
274
- en: {
275
- $delete: true,
276
- },
277
- },
278
- })
279
- t.deepEqual(resultCollect(r), [
280
- { path: ['text', 'en'], value: { $delete: true } },
281
- {
282
- path: ['text'],
283
- value: {
284
- en: {
285
- $delete: true,
286
- },
287
- },
288
- },
289
- ])
290
- })
291
-
292
- test('just delete', async (t) => {
293
- r = await setWalker(schema, {
294
- $id: 'bl120',
295
- $delete: true,
296
- })
297
- t.true(r.errors.length === 1)
298
- })
299
-
300
- test('$default in collected path', async (t) => {
301
- r = await setWalker(schema, {
302
- $id: 'bl120',
303
- text: {
304
- en: {
305
- $default: 'title',
306
- },
307
- },
308
- })
309
- t.is(r.errors.length, 0)
310
- t.deepEqual(resultCollect(r), [
311
- { path: ['text', 'en'], value: { $default: 'title' } },
312
- {
313
- path: ['text'],
314
- value: {
315
- en: {
316
- $default: 'title',
317
- },
318
- },
319
- },
320
- ])
321
- })
322
-
323
- test('$default in collected path + $merge:false', async (t) => {
324
- r = await setWalker(schema, {
325
- $id: 'bl120',
326
- text: {
327
- $merge: false,
328
- en: {
329
- $default: 'title',
330
- },
331
- },
332
- })
333
- t.is(r.errors.length, 0)
334
- t.deepEqual(resultCollect(r), [
335
- { path: ['text'], value: { $delete: true } },
336
-
337
- { path: ['text', 'en'], value: { $default: 'title' } },
338
- {
339
- path: ['text'],
340
- value: {
341
- $merge: false,
342
- en: {
343
- $default: 'title',
344
- },
345
- },
346
- },
347
- ])
348
- })
@@ -1,23 +0,0 @@
1
- import { Path } from '@saulx/utils'
2
- import { BasedSetTarget } from '../../src'
3
- import { ParseError } from '../../src/error'
4
-
5
- export const resultCollect = (...results: BasedSetTarget[]) => {
6
- const assertableResults: { path: Path; value: any }[] = []
7
- for (let i = 0; i < results.length; i++) {
8
- assertableResults.push(
9
- ...results[i].collected.map((v) => ({ path: v.path, value: v.value }))
10
- )
11
- }
12
- return assertableResults
13
- }
14
-
15
- export const errorCollect = (...results: BasedSetTarget[]) => {
16
- const errors: { path: Path; code: ParseError }[] = []
17
- for (let i = 0; i < results.length; i++) {
18
- if (results[i].errors) {
19
- errors.push(...results[i].errors)
20
- }
21
- }
22
- return errors
23
- }
@@ -1,41 +0,0 @@
1
- import test from 'ava'
2
- import { validateSchema } from '../src/index'
3
-
4
- test.serial('throw on invalid schema', async (t) => {
5
- const prefixError = t.throws(() => {
6
- validateSchema({
7
- $defs: {
8
- yuzi: {
9
- type: 'string',
10
- title: 'BLA',
11
- description: 'SNURP',
12
- },
13
- },
14
- types: {
15
- bla: {
16
- prefix: 'fix',
17
- fields: {
18
- yuzi: {
19
- type: 'object',
20
- customValidator: async (value, path, target) => {
21
- return true
22
- },
23
- properties: {
24
- gurt: {
25
- $ref: '/$defs/yuzi',
26
- },
27
- flap: {
28
- enum: ['bla', 'blap', 'flip'],
29
- },
30
- },
31
- },
32
- },
33
- },
34
- },
35
- })
36
- })
37
- t.is(
38
- prefixError.message,
39
- 'Incorrect prefix "fix" for type "bla" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"'
40
- )
41
- })