@based/schema 0.0.6 → 0.0.8

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.
Files changed (73) hide show
  1. package/dist/deepPartial.d.ts +0 -0
  2. package/dist/deepPartial.js +3 -0
  3. package/dist/deepPartial.js.map +1 -0
  4. package/dist/set/checkDefaultAndValue.d.ts +3 -0
  5. package/dist/set/checkDefaultAndValue.js +33 -0
  6. package/dist/set/checkDefaultAndValue.js.map +1 -0
  7. package/dist/set/collections.js +80 -56
  8. package/dist/set/collections.js.map +1 -1
  9. package/dist/set/enum.d.ts +2 -0
  10. package/dist/set/enum.js +15 -0
  11. package/dist/set/enum.js.map +1 -0
  12. package/dist/set/error.d.ts +4 -1
  13. package/dist/set/error.js +3 -0
  14. package/dist/set/error.js.map +1 -1
  15. package/dist/set/fieldValidator.d.ts +6 -0
  16. package/dist/set/fieldValidator.js +144 -0
  17. package/dist/set/fieldValidator.js.map +1 -0
  18. package/dist/set/handleError.d.ts +1 -0
  19. package/dist/set/handleError.js +9 -0
  20. package/dist/set/handleError.js.map +1 -0
  21. package/dist/set/index.d.ts +1 -1
  22. package/dist/set/index.js +5 -3
  23. package/dist/set/index.js.map +1 -1
  24. package/dist/set/number copy.d.ts +4 -0
  25. package/dist/set/number copy.js +57 -0
  26. package/dist/set/number copy.js.map +1 -0
  27. package/dist/set/number.js +86 -33
  28. package/dist/set/number.js.map +1 -1
  29. package/dist/set/parseDefaultAndValue.d.ts +3 -0
  30. package/dist/set/parseDefaultAndValue.js +33 -0
  31. package/dist/set/parseDefaultAndValue.js.map +1 -0
  32. package/dist/set/references.js +10 -14
  33. package/dist/set/references.js.map +1 -1
  34. package/dist/set/rest copy.d.ts +5 -0
  35. package/dist/set/rest copy.js +53 -0
  36. package/dist/set/rest copy.js.map +1 -0
  37. package/dist/set/rest.js +45 -22
  38. package/dist/set/rest.js.map +1 -1
  39. package/dist/set/string.js +41 -21
  40. package/dist/set/string.js.map +1 -1
  41. package/dist/set/types.d.ts +1 -1
  42. package/dist/setWalker.d.ts +11 -0
  43. package/dist/setWalker.js +189 -0
  44. package/dist/setWalker.js.map +1 -0
  45. package/dist/transformers.d.ts +3 -0
  46. package/dist/transformers.js +18 -0
  47. package/dist/transformers.js.map +1 -0
  48. package/dist/typeWalker.d.ts +3 -0
  49. package/dist/typeWalker.js +18 -0
  50. package/dist/typeWalker.js.map +1 -0
  51. package/dist/validate.d.ts +4 -0
  52. package/dist/validate.js +34 -0
  53. package/dist/validate.js.map +1 -0
  54. package/dist/validateFields.d.ts +4 -0
  55. package/dist/validateFields.js +34 -0
  56. package/dist/validateFields.js.map +1 -0
  57. package/dist/validateSchema copy.d.ts +4 -0
  58. package/dist/validateSchema copy.js +34 -0
  59. package/dist/validateSchema copy.js.map +1 -0
  60. package/package.json +1 -1
  61. package/src/set/collections.ts +111 -88
  62. package/src/set/error.ts +3 -0
  63. package/src/set/index.ts +6 -3
  64. package/src/set/number.ts +109 -37
  65. package/src/set/parseDefaultAndValue.ts +44 -0
  66. package/src/set/references.ts +14 -14
  67. package/src/set/rest.ts +80 -21
  68. package/src/set/string.ts +75 -23
  69. package/src/set/types.ts +3 -2
  70. package/test/setWalker.ts +93 -5
  71. package/dist/parse.d.ts +0 -2
  72. package/dist/parse.js +0 -9
  73. package/dist/parse.js.map +0 -1
package/src/set/rest.ts CHANGED
@@ -2,6 +2,7 @@ import { Parser } from './types'
2
2
  import { deepEqual } from '@saulx/utils'
3
3
  import { hashObjectIgnoreKeyOrder, hash } from '@saulx/hash'
4
4
  import { error, ParseError } from './error'
5
+ import { parseValueAndDefault } from './parseDefaultAndValue'
5
6
 
6
7
  export const cardinality: Parser<'cardinality'> = async (
7
8
  path,
@@ -9,14 +10,24 @@ export const cardinality: Parser<'cardinality'> = async (
9
10
  fieldSchema,
10
11
  typeSchema,
11
12
  target,
12
- handlers
13
+ handlers,
14
+ noCollect
13
15
  ) => {
14
16
  if (value && typeof value === 'object') {
15
- value = hashObjectIgnoreKeyOrder(value).toString(16)
17
+ if (value.$default !== undefined) {
18
+ error(path, ParseError.defaultNotSupported)
19
+ }
20
+ if (value.$value !== undefined) {
21
+ value = hashObjectIgnoreKeyOrder(value.$value).toString(16)
22
+ } else {
23
+ value = hashObjectIgnoreKeyOrder(value).toString(16)
24
+ }
16
25
  } else {
17
26
  value = hash(value).toString(16)
18
27
  }
19
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
28
+ if (!noCollect) {
29
+ handlers.collect({ path, value, typeSchema, fieldSchema, target })
30
+ }
20
31
  }
21
32
 
22
33
  export const boolean: Parser<'boolean'> = async (
@@ -25,14 +36,30 @@ export const boolean: Parser<'boolean'> = async (
25
36
  fieldSchema,
26
37
  typeSchema,
27
38
  target,
28
- handlers
39
+ handlers,
40
+ noCollect
29
41
  ) => {
30
- // value .default
31
- // $increment / $decrement
42
+ if (
43
+ await parseValueAndDefault(
44
+ path,
45
+ value,
46
+ fieldSchema,
47
+ typeSchema,
48
+ target,
49
+ handlers,
50
+ noCollect
51
+ )
52
+ ) {
53
+ return
54
+ }
55
+
32
56
  if (typeof value !== 'boolean') {
33
57
  error(path, ParseError.incorrectFormat)
34
58
  }
35
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
59
+
60
+ if (!noCollect) {
61
+ handlers.collect({ path, value, typeSchema, fieldSchema, target })
62
+ }
36
63
  }
37
64
 
38
65
  export const enumParser: Parser<'enum'> = async (
@@ -41,16 +68,31 @@ export const enumParser: Parser<'enum'> = async (
41
68
  fieldSchema,
42
69
  typeSchema,
43
70
  target,
44
- handlers
71
+ handlers,
72
+ noCollect
45
73
  ) => {
46
74
  const enumValues = fieldSchema.enum
47
- for (let i = 0; i < enumValues.length; i++) {
48
- if (deepEqual(enumValues[i], value)) {
49
- handlers.collect({ path, value: i, typeSchema, fieldSchema, target })
50
- return
75
+ if (
76
+ !(await parseValueAndDefault(
77
+ path,
78
+ value,
79
+ fieldSchema,
80
+ typeSchema,
81
+ target,
82
+ handlers,
83
+ noCollect
84
+ ))
85
+ ) {
86
+ for (let i = 0; i < enumValues.length; i++) {
87
+ if (deepEqual(enumValues[i], value)) {
88
+ if (!noCollect) {
89
+ handlers.collect({ path, value: i, typeSchema, fieldSchema, target })
90
+ }
91
+ return
92
+ }
51
93
  }
94
+ error(path, ParseError.incorrectFormat)
52
95
  }
53
- error(path, ParseError.incorrectFormat)
54
96
  }
55
97
 
56
98
  export const json: Parser<'json'> = async (
@@ -59,18 +101,35 @@ export const json: Parser<'json'> = async (
59
101
  fieldSchema,
60
102
  typeSchema,
61
103
  target,
62
- handlers
104
+ handlers,
105
+ noCollect
63
106
  ) => {
64
- try {
65
- const parsedValue = JSON.stringify(value)
66
- handlers.collect({
107
+ if (
108
+ await parseValueAndDefault(
67
109
  path,
68
- value: parsedValue,
69
- typeSchema,
110
+ value,
70
111
  fieldSchema,
112
+ typeSchema,
71
113
  target,
72
- })
114
+ handlers,
115
+ noCollect
116
+ )
117
+ ) {
118
+ return
119
+ }
120
+
121
+ try {
122
+ const parsedValue = JSON.stringify(value)
123
+ if (!noCollect) {
124
+ handlers.collect({
125
+ path,
126
+ value: parsedValue,
127
+ typeSchema,
128
+ fieldSchema,
129
+ target,
130
+ })
131
+ }
73
132
  } catch (err) {
74
- throw err(path, ParseError.incorrectFormat)
133
+ error(path, ParseError.incorrectFormat)
75
134
  }
76
135
  }
package/src/set/string.ts CHANGED
@@ -2,6 +2,7 @@ import { Parser } from './types'
2
2
  import { error, ParseError } from './error'
3
3
  import { BasedSchemaFieldString, BasedSchemaFieldText } from '../types'
4
4
  import validators from 'validator'
5
+ import { parseValueAndDefault } from './parseDefaultAndValue'
5
6
 
6
7
  const formatPatterns: Record<
7
8
  BasedSchemaFieldString['format'],
@@ -101,10 +102,26 @@ export const string: Parser<'string'> = async (
101
102
  fieldSchema,
102
103
  typeSchema,
103
104
  target,
104
- handlers
105
+ handlers,
106
+ noCollect
105
107
  ) => {
108
+ if (
109
+ await parseValueAndDefault(
110
+ path,
111
+ value,
112
+ fieldSchema,
113
+ typeSchema,
114
+ target,
115
+ handlers,
116
+ noCollect
117
+ )
118
+ ) {
119
+ return
120
+ }
106
121
  validate(path, value, fieldSchema)
107
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
122
+ if (!noCollect) {
123
+ handlers.collect({ path, value, typeSchema, fieldSchema, target })
124
+ }
108
125
  }
109
126
 
110
127
  export const text: Parser<'text'> = async (
@@ -113,18 +130,21 @@ export const text: Parser<'text'> = async (
113
130
  fieldSchema,
114
131
  typeSchema,
115
132
  target,
116
- handlers
133
+ handlers,
134
+ noCollect
117
135
  ) => {
118
136
  const valueType = typeof value
119
137
  if (target.$language && valueType === 'string') {
120
138
  validate(path, value, fieldSchema)
121
- handlers.collect({
122
- path,
123
- value: { [target.$language]: value },
124
- typeSchema,
125
- fieldSchema,
126
- target,
127
- })
139
+ if (!noCollect) {
140
+ handlers.collect({
141
+ path,
142
+ value: { [target.$language]: value },
143
+ typeSchema,
144
+ fieldSchema,
145
+ target,
146
+ })
147
+ }
128
148
  return
129
149
  }
130
150
 
@@ -132,28 +152,60 @@ export const text: Parser<'text'> = async (
132
152
  error(path, ParseError.incorrectFormat)
133
153
  }
134
154
 
155
+ if (
156
+ target.$language &&
157
+ (await parseValueAndDefault(
158
+ path,
159
+ value,
160
+ fieldSchema,
161
+ typeSchema,
162
+ target,
163
+ handlers,
164
+ noCollect
165
+ ))
166
+ ) {
167
+ return
168
+ }
169
+
135
170
  for (const key in value) {
136
171
  const newPath = [...path, key]
137
172
 
138
- if (typeof value[key] === 'object' && value[key].$delete === true) {
173
+ if (typeof value[key] === 'object') {
174
+ if (value[key].$value) {
175
+ text(
176
+ [...path, key, '$value'],
177
+ value[key].$value,
178
+ fieldSchema,
179
+ typeSchema,
180
+ target,
181
+ handlers,
182
+ true
183
+ )
184
+ }
185
+ if (!noCollect) {
186
+ handlers.collect({
187
+ path: newPath,
188
+ value: null,
189
+ typeSchema,
190
+ fieldSchema,
191
+ target,
192
+ })
193
+ }
194
+ continue
195
+ }
196
+
197
+ // if
198
+
199
+ // validate(newPath, value[key], fieldSchema)
200
+
201
+ if (!noCollect) {
139
202
  handlers.collect({
140
203
  path: newPath,
141
- value: null,
204
+ value: value[key],
142
205
  typeSchema,
143
206
  fieldSchema,
144
207
  target,
145
208
  })
146
- continue
147
209
  }
148
-
149
- validate(newPath, value[key], fieldSchema)
150
-
151
- handlers.collect({
152
- path: newPath,
153
- value: value[key],
154
- typeSchema,
155
- fieldSchema,
156
- target,
157
- })
158
210
  }
159
211
  }
package/src/set/types.ts CHANGED
@@ -11,8 +11,9 @@ export type Parser<K extends keyof BasedSchemaFields> = (
11
11
  fieldSchema: BasedSchemaFields[K],
12
12
  typeSchema: BasedSchemaType,
13
13
  target: BasedSetTarget,
14
- handlers: BasedSetHandlers
15
- ) => Promise<void>
14
+ handlers: BasedSetHandlers,
15
+ noCollect?: boolean
16
+ ) => Promise<void | boolean>
16
17
 
17
18
  export type Parsers = {
18
19
  [Key in keyof BasedSchemaFields]: Parser<Key>
package/test/setWalker.ts CHANGED
@@ -9,6 +9,12 @@ const schema: BasedSchema = {
9
9
  visits: {
10
10
  type: 'cardinality',
11
11
  },
12
+ blub: {
13
+ type: 'number',
14
+ },
15
+ flap: {
16
+ type: 'number',
17
+ },
12
18
  snurp: {
13
19
  type: 'array',
14
20
  values: {
@@ -149,6 +155,9 @@ test.serial('collect correctly', async (t) => {
149
155
  snurp: false,
150
156
  ua: '123435',
151
157
  },
158
+ blub: {
159
+ $increment: 1,
160
+ },
152
161
  bla: false,
153
162
  time: now, // do more later
154
163
  setje: [1, 2, 3],
@@ -203,16 +212,17 @@ test.serial('collect correctly', async (t) => {
203
212
 
204
213
  const result = [
205
214
  { path: ['visits'], value: '3a9009740ee' },
206
- { path: ['bla'], value: false },
215
+ { path: ['blub'], value: { $increment: 1 } },
207
216
  { path: ['time'], value: now },
208
- { path: ['form', 'lastName'], value: 'de beer' },
209
- { path: ['form', 'json'], value: '{"bla":1,"x":2,"y":3}' },
210
217
  { path: ['form', 'snurp'], value: 'blx12' },
211
- { path: ['form', 'things'], value: 2 },
212
- { path: ['form', 'password'], value: 'mypassword!' },
213
218
  { path: ['snurp', 0, 'x', 0], value: 1 },
214
219
  { path: ['snurp', 0, 'x', 1], value: 2 },
215
220
  { path: ['snurp', 0, 'x', 2], value: 3 },
221
+ { path: ['bla'], value: false },
222
+ { path: ['form', 'lastName'], value: 'de beer' },
223
+ { path: ['form', 'json'], value: '{"bla":1,"x":2,"y":3}' },
224
+ { path: ['form', 'things'], value: 2 },
225
+ { path: ['form', 'password'], value: 'mypassword!' },
216
226
  { path: ['form', 'bla'], value: { $value: ['bl123', 'bl234'] } },
217
227
  { path: ['form', 'blab'], value: { $add: ['bl456'] } },
218
228
  {
@@ -227,5 +237,83 @@ test.serial('collect correctly', async (t) => {
227
237
  },
228
238
  ]
229
239
 
240
+ console.log(results)
241
+
230
242
  t.deepEqual(results, result)
243
+
244
+ console.info('DID COMPARSION!')
245
+
246
+ const results2: any[] = []
247
+ await setWalker(
248
+ schema,
249
+ {
250
+ $id: 'bl1',
251
+ blub: {
252
+ $value: 4,
253
+ },
254
+ flap: {
255
+ $default: 1,
256
+ },
257
+ },
258
+ {
259
+ collect: ({ path, value, typeSchema, fieldSchema, target }) => {
260
+ console.dir(
261
+ {
262
+ path,
263
+ value,
264
+ },
265
+ { depth: 10 }
266
+ )
267
+ results2.push({
268
+ path,
269
+ value,
270
+ })
271
+ },
272
+ referenceFilterCondition: async (id, filter) => {
273
+ return true
274
+ },
275
+ }
276
+ )
277
+
278
+ t.deepEqual(results2, [
279
+ { path: ['blub'], value: { $value: 4 } },
280
+ { path: ['flap'], value: { $default: 1 } },
281
+ ])
282
+
283
+ const results3: any[] = []
284
+ await setWalker(
285
+ schema,
286
+ {
287
+ $id: 'bl1',
288
+ snurpArray: {
289
+ $push: 1,
290
+ },
291
+ specialArray: {
292
+ $push: { $value: 'flap' },
293
+ },
294
+ },
295
+ {
296
+ collect: ({ path, value, typeSchema, fieldSchema, target }) => {
297
+ console.dir(
298
+ {
299
+ path,
300
+ value,
301
+ },
302
+ { depth: 10 }
303
+ )
304
+ results3.push({
305
+ path,
306
+ value,
307
+ })
308
+ },
309
+ referenceFilterCondition: async (id, filter) => {
310
+ return true
311
+ },
312
+ }
313
+ )
314
+
315
+ t.deepEqual(results3, [
316
+ { path: ['snurpArray'], value: { $push: [1] } },
317
+ { path: ['specialArray'], value: { $push: [{ $value: 'flap' }] } },
318
+ ])
231
319
  })
package/dist/parse.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { BasedSchema } from './schema';
2
- export declare const parseSchema: (schema: BasedSchema) => BasedSchema;
package/dist/parse.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseSchema = void 0;
4
- const parseSchema = (schema) => {
5
- // rewrite schema things like required / required: []
6
- return schema;
7
- };
8
- exports.parseSchema = parseSchema;
9
- //# sourceMappingURL=parse.js.map
package/dist/parse.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":";;;AAEO,MAAM,WAAW,GAAG,CAAC,MAAmB,EAAe,EAAE;IAC9D,qDAAqD;IAErD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAJY,QAAA,WAAW,eAIvB"}