@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/rest.ts DELETED
@@ -1,185 +0,0 @@
1
- import test from 'ava'
2
- import { BasedSchema, setWalker } from '../src/index'
3
- import { errorCollect, resultCollect } from './utils'
4
-
5
- const schema: BasedSchema = {
6
- types: {
7
- thing: {
8
- prefix: 'ti',
9
- fields: {
10
- priority: { type: 'number' },
11
- something: { type: 'string', format: 'strongPassword' },
12
- },
13
- },
14
- bla: {
15
- prefix: 'bl',
16
- fields: {
17
- enum: {
18
- enum: ['tony', 'jim'],
19
- },
20
- setOfNumbers: {
21
- type: 'set',
22
- items: {
23
- type: 'number',
24
- },
25
- },
26
- object: {
27
- type: 'object',
28
- properties: {
29
- flap: { type: 'boolean' },
30
- },
31
- },
32
- flap: {
33
- type: 'boolean',
34
- },
35
- x: {
36
- type: 'object',
37
- properties: {
38
- flap: {
39
- type: 'boolean',
40
- },
41
- },
42
- },
43
- record: {
44
- type: 'record',
45
- values: {
46
- type: 'object',
47
- properties: {
48
- bla: {
49
- type: 'array',
50
- values: {
51
- type: 'object',
52
- properties: {
53
- snux: {
54
- type: 'object',
55
- properties: {
56
- x: {
57
- type: 'number',
58
- },
59
- },
60
- },
61
- flap: { type: 'number' },
62
- },
63
- },
64
- },
65
- },
66
- },
67
- },
68
- bla: {
69
- type: 'set',
70
- items: { type: 'string', minLength: 3, maxLength: 6 },
71
- },
72
- },
73
- },
74
- },
75
- $defs: {},
76
- language: 'en',
77
- translations: ['de', 'nl', 'ro', 'za', 'ae'],
78
- root: {
79
- fields: {},
80
- },
81
- prefixToTypeMapping: {
82
- bl: 'bla',
83
- ti: 'thing',
84
- },
85
- }
86
-
87
- let r
88
-
89
- test('enum simple', async (t) => {
90
- r = await setWalker(schema, {
91
- $id: 'bl120',
92
- enum: 'tony',
93
- })
94
-
95
- t.true(r.errors.length === 0)
96
- t.deepEqual(resultCollect(r), [{ path: ['enum'], value: 0 }])
97
- })
98
-
99
- test('enum not exist error', async (t) => {
100
- r = await setWalker(schema, {
101
- $id: 'bl120',
102
- enum: 'kyle',
103
- })
104
- t.true(r.errors.length === 1)
105
- })
106
-
107
- test('default enum', async (t) => {
108
- r = await setWalker(schema, {
109
- $id: 'bl120',
110
- enum: { $default: 'tony' },
111
- })
112
- t.true(r.errors.length === 0)
113
- t.deepEqual(resultCollect(r), [{ path: ['enum'], value: { $default: 0 } }])
114
- })
115
-
116
- test('value enum ', async (t) => {
117
- r = await setWalker(schema, {
118
- $id: 'bl120',
119
- enum: { $value: 'tony' },
120
- })
121
-
122
- t.true(r.errors.length === 0)
123
- t.deepEqual(resultCollect(r), [{ path: ['enum'], value: 0 }])
124
- })
125
-
126
- test('value & default enum ', async (t) => {
127
- r = await setWalker(schema, {
128
- $id: 'bl120',
129
- enum: { $default: 'jim', $value: 'tony' },
130
- })
131
-
132
- t.true(r.errors.length === 2)
133
- })
134
-
135
- test('$value & $default enum ', async (t) => {
136
- r = await setWalker(schema, {
137
- $id: 'bl120',
138
- enum: { $value: { $default: 'tony' } },
139
- })
140
- t.true(r.errors.length === 0)
141
- t.deepEqual(resultCollect(r), [{ path: ['enum'], value: { $default: 0 } }])
142
- })
143
-
144
- test('default: value enum ', async (t) => {
145
- r = await setWalker(schema, {
146
- $id: 'bl120',
147
- enum: { $default: { $value: 'tony' } },
148
- })
149
- t.true(r.errors.length === 0)
150
- t.deepEqual(resultCollect(r), [{ path: ['enum'], value: { $default: 0 } }])
151
- })
152
-
153
- // ask about alias
154
- test('$alias ', async (t) => {
155
- r = await setWalker(schema, {
156
- $id: 'bl120',
157
- $alias: 'bla',
158
- })
159
-
160
- t.true(r.errors.length === 0)
161
- })
162
-
163
- test('$alias array', async (t) => {
164
- r = await setWalker(schema, {
165
- $id: 'bl120',
166
- $alias: ['bla'],
167
- })
168
-
169
- t.true(r.errors.length === 0)
170
- })
171
-
172
- test('object: boolean', async (t) => {
173
- r = await setWalker(schema, {
174
- $id: 'bl120',
175
- object: {
176
- flap: true,
177
- },
178
- })
179
-
180
- t.true(r.errors.length === 0)
181
- t.deepEqual(resultCollect(r), [
182
- { path: ['object', 'flap'], value: true },
183
- { path: ['object'], value: { flap: true } },
184
- ])
185
- })
package/test/set.ts DELETED
@@ -1,104 +0,0 @@
1
- import test from 'ava'
2
- import { BasedSchema, setWalker, walk } from '../src/index'
3
- import { wait } from '@saulx/utils'
4
- import { resultCollect } from './utils'
5
- let r
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
- x: {
18
- type: 'object',
19
- properties: {
20
- bla: { type: 'string' },
21
- },
22
- },
23
- setOfNumbers: {
24
- type: 'set',
25
- items: {
26
- type: 'number',
27
- },
28
- },
29
- bla: {
30
- type: 'set',
31
- items: { type: 'string', minLength: 3, maxLength: 6 },
32
- },
33
- },
34
- },
35
- },
36
- $defs: {},
37
- language: 'en',
38
- translations: ['de', 'nl', 'ro', 'za', 'ae'],
39
- root: {
40
- fields: {},
41
- },
42
- prefixToTypeMapping: {
43
- bl: 'bla',
44
- ti: 'thing',
45
- },
46
- }
47
-
48
- test('simple setNum', async (t) => {
49
- r = await setWalker(schema, {
50
- $id: 'bl120',
51
- setOfNumbers: [1, 2, 3, 4, 5],
52
- })
53
-
54
- t.true(r.errors.length === 0)
55
- t.deepEqual(resultCollect(r), [
56
- { path: ['setOfNumbers'], value: { $value: [1, 2, 3, 4, 5] } },
57
- ])
58
- })
59
-
60
- test('default arr', async (t) => {
61
- r = await setWalker(schema, {
62
- $id: 'bl120',
63
- setOfNumbers: { $add: 20 },
64
- })
65
-
66
- t.true(r.errors.length === 0)
67
- t.deepEqual(resultCollect(r), [
68
- { path: ['setOfNumbers'], value: { $add: [20] } },
69
- ])
70
- })
71
-
72
- test('$merge on object', async (t) => {
73
- r = await setWalker(schema, {
74
- $id: 'bl120',
75
- x: {
76
- $merge: false,
77
- bla: 'x',
78
- },
79
- })
80
-
81
- t.true(r.errors.length === 0)
82
- t.deepEqual(resultCollect(r), [
83
- { path: ['x'], value: { $delete: true } },
84
-
85
- { path: ['x', 'bla'], value: 'x' },
86
-
87
- { path: ['x'], value: { $merge: false, bla: 'x' } },
88
- ])
89
- })
90
-
91
- test('$merge on set', async (t) => {
92
- r = await setWalker(schema, {
93
- $id: 'bl120',
94
- $merge: false,
95
- x: {
96
- bla: 'x',
97
- },
98
- })
99
- t.true(r.errors.length === 0)
100
- t.deepEqual(resultCollect(r), [
101
- { path: ['x', 'bla'], value: 'x' },
102
- { path: ['x'], value: { bla: 'x' } },
103
- ])
104
- })
package/test/string.ts DELETED
@@ -1,118 +0,0 @@
1
- import test from 'ava'
2
- import { BasedSchema, setWalker } from '../src/index'
3
- import { errorCollect, resultCollect } from './utils'
4
-
5
- const schema: BasedSchema = {
6
- types: {
7
- bla: {
8
- prefix: 'bl',
9
- fields: {
10
- name: {
11
- minLength: 3,
12
- maxLength: 6,
13
- type: 'string',
14
- },
15
- phonkName: {
16
- type: 'string',
17
- pattern: '\\${1,4}',
18
- },
19
- bla: {
20
- type: 'set',
21
- items: { type: 'string', minLength: 3, maxLength: 6 },
22
- },
23
- },
24
- },
25
- },
26
- $defs: {},
27
- language: 'en',
28
- root: {
29
- fields: {},
30
- },
31
- prefixToTypeMapping: {
32
- bl: 'bla',
33
- },
34
- }
35
-
36
- test('string max length', async (t) => {
37
- const err1 = await setWalker(schema, {
38
- $id: 'bl1',
39
- name: 'ax',
40
- })
41
-
42
- const err2 = await setWalker(schema, {
43
- $id: 'bl1',
44
- name: 'axaxaxax',
45
- })
46
- const res1 = await setWalker(schema, {
47
- $id: 'bl1',
48
- name: 'xaxx',
49
- })
50
-
51
- t.assert(errorCollect(err1, err2).length > 0)
52
- t.deepEqual(resultCollect(res1), [{ path: ['name'], value: 'xaxx' }])
53
- })
54
-
55
- //set parser
56
- test('set with strings', async (t) => {
57
- const err1 = await setWalker(schema, {
58
- $id: 'bl1',
59
- bla: ['ax', 'axa', 'axxxxa'],
60
- })
61
-
62
- const res1 = await setWalker(schema, {
63
- $id: 'bl1',
64
- bla: ['axx', 'axxxx', 'blaaa'],
65
- })
66
-
67
- t.assert(errorCollect(err1).length > 0)
68
- t.deepEqual(resultCollect(res1), [
69
- { path: ['bla'], value: { $value: ['axx', 'axxxx', 'blaaa'] } },
70
- ])
71
- })
72
-
73
- // this one causes weird array lenght issue in string max length test
74
- test('string pattern', async (t) => {
75
- const err1 = await setWalker(schema, {
76
- $id: 'bl1',
77
- phonkName: 'blabla',
78
- })
79
-
80
- const res1 = await setWalker(schema, {
81
- $id: 'bl1',
82
- phonkName: 'bla$',
83
- })
84
-
85
- t.assert(errorCollect(err1).length > 0)
86
- t.deepEqual(resultCollect(res1), [{ path: ['phonkName'], value: 'bla$' }])
87
- })
88
-
89
- test('setting $default', async (t) => {
90
- const err = await setWalker(schema, {
91
- $id: 'bl1',
92
- phonkName: { $default: 'blabla' },
93
- })
94
-
95
- t.assert(err.errors.length > 0)
96
-
97
- const res1 = await setWalker(schema, {
98
- $id: 'bl1',
99
- phonkName: { $default: 'bla$' },
100
- })
101
-
102
- t.deepEqual(resultCollect(res1), [
103
- { path: ['phonkName'], value: { $default: 'bla$' } },
104
- ])
105
- })
106
-
107
- test('setting $value', async (t) => {
108
- const err = await setWalker(schema, {
109
- $id: 'bl1',
110
- phonkName: { $value: 'blabla' },
111
- })
112
- t.is(err.errors.length, 1)
113
- const res1 = await setWalker(schema, {
114
- $id: 'bl1',
115
- phonkName: { $value: 'bla$' },
116
- })
117
- t.deepEqual(resultCollect(res1), [{ path: ['phonkName'], value: 'bla$' }])
118
- })