@based/schema 0.0.6 → 0.0.7

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 (62) 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/collections.js +37 -53
  5. package/dist/set/collections.js.map +1 -1
  6. package/dist/set/enum.d.ts +2 -0
  7. package/dist/set/enum.js +15 -0
  8. package/dist/set/enum.js.map +1 -0
  9. package/dist/set/fieldValidator.d.ts +6 -0
  10. package/dist/set/fieldValidator.js +144 -0
  11. package/dist/set/fieldValidator.js.map +1 -0
  12. package/dist/set/handleError.d.ts +1 -0
  13. package/dist/set/handleError.js +9 -0
  14. package/dist/set/handleError.js.map +1 -0
  15. package/dist/set/index.d.ts +1 -1
  16. package/dist/set/index.js +5 -3
  17. package/dist/set/index.js.map +1 -1
  18. package/dist/set/number copy.d.ts +4 -0
  19. package/dist/set/number copy.js +57 -0
  20. package/dist/set/number copy.js.map +1 -0
  21. package/dist/set/number.js +72 -31
  22. package/dist/set/number.js.map +1 -1
  23. package/dist/set/references.js +10 -14
  24. package/dist/set/references.js.map +1 -1
  25. package/dist/set/rest copy.d.ts +5 -0
  26. package/dist/set/rest copy.js +53 -0
  27. package/dist/set/rest copy.js.map +1 -0
  28. package/dist/set/rest.js +18 -14
  29. package/dist/set/rest.js.map +1 -1
  30. package/dist/set/string.js +34 -21
  31. package/dist/set/string.js.map +1 -1
  32. package/dist/set/types.d.ts +1 -1
  33. package/dist/setWalker.d.ts +11 -0
  34. package/dist/setWalker.js +189 -0
  35. package/dist/setWalker.js.map +1 -0
  36. package/dist/transformers.d.ts +3 -0
  37. package/dist/transformers.js +18 -0
  38. package/dist/transformers.js.map +1 -0
  39. package/dist/typeWalker.d.ts +3 -0
  40. package/dist/typeWalker.js +18 -0
  41. package/dist/typeWalker.js.map +1 -0
  42. package/dist/validate.d.ts +4 -0
  43. package/dist/validate.js +34 -0
  44. package/dist/validate.js.map +1 -0
  45. package/dist/validateFields.d.ts +4 -0
  46. package/dist/validateFields.js +34 -0
  47. package/dist/validateFields.js.map +1 -0
  48. package/dist/validateSchema copy.d.ts +4 -0
  49. package/dist/validateSchema copy.js +34 -0
  50. package/dist/validateSchema copy.js.map +1 -0
  51. package/package.json +1 -1
  52. package/src/set/collections.ts +51 -82
  53. package/src/set/index.ts +6 -3
  54. package/src/set/number.ts +93 -35
  55. package/src/set/references.ts +14 -14
  56. package/src/set/rest.ts +21 -14
  57. package/src/set/string.ts +48 -23
  58. package/src/set/types.ts +2 -1
  59. package/test/setWalker.ts +7 -0
  60. package/dist/parse.d.ts +0 -2
  61. package/dist/parse.js +0 -9
  62. package/dist/parse.js.map +0 -1
package/src/set/index.ts CHANGED
@@ -14,7 +14,8 @@ export const fieldWalker = async (
14
14
  fieldSchema: BasedSchemaField,
15
15
  typeSchema: BasedSchemaType,
16
16
  target: BasedSetTarget,
17
- handlers: BasedSetHandlers
17
+ handlers: BasedSetHandlers,
18
+ noCollect?: boolean
18
19
  ): Promise<void> => {
19
20
  if ('$ref' in fieldSchema) {
20
21
  // TODO: when we have this it has to get it from the schema and redo the parsing with the correct fieldSchema
@@ -24,7 +25,9 @@ export const fieldWalker = async (
24
25
 
25
26
  const valueIsObject = value && valueType === 'object'
26
27
  if (valueIsObject && value.$delete === true) {
27
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
28
+ if (!noCollect) {
29
+ handlers.collect({ path, value, typeSchema, fieldSchema, target })
30
+ }
28
31
  return
29
32
  }
30
33
 
@@ -43,7 +46,7 @@ export const fieldWalker = async (
43
46
 
44
47
  const parse = parsers[typeDef]
45
48
 
46
- await parse(path, value, fieldSchema, typeSchema, target, handlers)
49
+ await parse(path, value, fieldSchema, typeSchema, target, handlers, noCollect)
47
50
 
48
51
  return
49
52
  }
package/src/set/number.ts CHANGED
@@ -1,5 +1,62 @@
1
1
  import { Parser } from './types'
2
2
  import { error, ParseError } from './error'
3
+ import {
4
+ BasedSchemaFieldInteger,
5
+ BasedSchemaFieldNumber,
6
+ BasedSchemaFieldTimeStamp,
7
+ } from '../types'
8
+
9
+ const validate = (
10
+ path: (number | string)[],
11
+ value: any,
12
+ fieldSchema:
13
+ | BasedSchemaFieldInteger
14
+ | BasedSchemaFieldNumber
15
+ | BasedSchemaFieldTimeStamp
16
+ ): number => {
17
+ if (typeof value !== 'number') {
18
+ error(path, ParseError.incorrectFormat)
19
+ }
20
+ if (fieldSchema.maximum) {
21
+ if (fieldSchema.exclusiveMaximum && value > value) {
22
+ error(path, ParseError.exceedsMaximum)
23
+ } else if (value >= value) {
24
+ error(path, ParseError.exceedsMaximum)
25
+ }
26
+ }
27
+ if (fieldSchema.minimum) {
28
+ if (fieldSchema.exclusiveMinimum && value < value) {
29
+ error(path, ParseError.subceedsMinimum)
30
+ } else if (value <= value) {
31
+ error(path, ParseError.subceedsMinimum)
32
+ }
33
+ }
34
+ return value
35
+ }
36
+
37
+ const shared = (
38
+ path: (number | string)[],
39
+ value: any,
40
+ fieldSchema:
41
+ | BasedSchemaFieldInteger
42
+ | BasedSchemaFieldNumber
43
+ | BasedSchemaFieldTimeStamp
44
+ ): any => {
45
+ if (typeof value === 'object') {
46
+ if (value.$increment) {
47
+ validate([...path, '$increment'], value.$increment, fieldSchema)
48
+ }
49
+ if (value.$decrement) {
50
+ validate([...path, '$decrement'], value.$decrement, fieldSchema)
51
+ }
52
+ if (value.$value) {
53
+ validate([...path], value.$value, fieldSchema)
54
+ }
55
+ } else {
56
+ validate(path, value, fieldSchema)
57
+ }
58
+ return value
59
+ }
3
60
 
4
61
  export const timestamp: Parser<'timestamp'> = async (
5
62
  path,
@@ -7,7 +64,8 @@ export const timestamp: Parser<'timestamp'> = async (
7
64
  fieldSchema,
8
65
  typeSchema,
9
66
  target,
10
- handlers
67
+ handlers,
68
+ noCollect
11
69
  ) => {
12
70
  if (typeof value === 'string') {
13
71
  if (value === 'now') {
@@ -20,27 +78,16 @@ export const timestamp: Parser<'timestamp'> = async (
20
78
  }
21
79
  }
22
80
  }
23
- // // smaller then / larger then steps
24
- // if (typeof value !== 'number') {
25
- // throw createError(path, target.type, 'timestamp', value)
26
- // }
27
-
28
- // if (fieldSchema.maximum) {
29
- // if (fieldSchema.exclusiveMaximum && value > value) {
30
- // throw createError(path, target.type, 'timestamp', value)
31
- // } else if (value >= value) {
32
- // throw createError(path, target.type, 'timestamp', value)
33
- // }
34
- // }
35
-
36
- // if (fieldSchema.minimum) {
37
- // if (fieldSchema.exclusiveMinimum && value < value) {
38
- // throw createError(path, target.type, 'timestamp', value)
39
- // } else if (value <= value) {
40
- // throw createError(path, target.type, 'timestamp', value)
41
- // }
42
- // }
43
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
81
+ const parsedValue = shared(path, value, fieldSchema)
82
+ if (!noCollect) {
83
+ handlers.collect({
84
+ path,
85
+ value: parsedValue,
86
+ typeSchema,
87
+ fieldSchema,
88
+ target,
89
+ })
90
+ }
44
91
  }
45
92
 
46
93
  export const number: Parser<'number'> = async (
@@ -49,16 +96,19 @@ export const number: Parser<'number'> = async (
49
96
  fieldSchema,
50
97
  typeSchema,
51
98
  target,
52
- handlers
99
+ handlers,
100
+ noCollect
53
101
  ) => {
54
- // value .default
55
- // $increment / $decrement
56
-
57
- if (typeof value !== 'number') {
58
- error(path, ParseError.incorrectFormat)
102
+ const parsedValue = shared(path, value, fieldSchema)
103
+ if (!noCollect) {
104
+ handlers.collect({
105
+ path,
106
+ value: parsedValue,
107
+ typeSchema,
108
+ fieldSchema,
109
+ target,
110
+ })
59
111
  }
60
-
61
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
62
112
  }
63
113
 
64
114
  export const integer: Parser<'integer'> = async (
@@ -67,12 +117,20 @@ export const integer: Parser<'integer'> = async (
67
117
  fieldSchema,
68
118
  typeSchema,
69
119
  target,
70
- handlers
120
+ handlers,
121
+ noCollect
71
122
  ) => {
72
- // value .default
73
- // $increment / $decrement
74
- if (typeof value !== 'number' || value - Math.floor(value) !== 0) {
123
+ if (typeof value === 'number' && value - Math.floor(value) !== 0) {
75
124
  error(path, ParseError.incorrectFormat)
76
125
  }
77
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
126
+ const parsedValue = shared(path, value, fieldSchema)
127
+ if (!noCollect) {
128
+ handlers.collect({
129
+ path,
130
+ value: parsedValue,
131
+ typeSchema,
132
+ fieldSchema,
133
+ target,
134
+ })
135
+ }
78
136
  }
@@ -7,7 +7,8 @@ export const reference: Parser<'reference'> = async (
7
7
  fieldSchema,
8
8
  typeSchema,
9
9
  target,
10
- handlers
10
+ handlers,
11
+ noCollect
11
12
  ) => {
12
13
  // $no root
13
14
  // prob pass these as options
@@ -50,7 +51,9 @@ export const reference: Parser<'reference'> = async (
50
51
  error(path, ParseError.referenceIsIncorrectType)
51
52
  }
52
53
  }
53
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
54
+ if (!noCollect) {
55
+ handlers.collect({ path, value, typeSchema, fieldSchema, target })
56
+ }
54
57
  }
55
58
 
56
59
  export const references: Parser<'references'> = async (
@@ -59,15 +62,12 @@ export const references: Parser<'references'> = async (
59
62
  fieldSchema,
60
63
  typeSchema,
61
64
  target,
62
- handlers
65
+ handlers,
66
+ noCollect
63
67
  ) => {
64
68
  // default
65
69
  // $no root
66
70
  if (Array.isArray(value)) {
67
- const handler = {
68
- ...handlers,
69
- collect: () => {},
70
- }
71
71
  await Promise.all(
72
72
  value.map((v, i) => {
73
73
  return reference(
@@ -77,17 +77,14 @@ export const references: Parser<'references'> = async (
77
77
  { ...fieldSchema, type: 'reference' },
78
78
  typeSchema,
79
79
  target,
80
- handler
80
+ handlers,
81
+ true
81
82
  )
82
83
  })
83
84
  )
84
85
  value = { $value: value }
85
86
  } else if (typeof value === 'object') {
86
87
  if (value.$add) {
87
- const handler = {
88
- ...handlers,
89
- collect: () => {},
90
- }
91
88
  await Promise.all(
92
89
  value.$add.map((v, i) => {
93
90
  return reference(
@@ -97,11 +94,14 @@ export const references: Parser<'references'> = async (
97
94
  { ...fieldSchema, type: 'reference' },
98
95
  typeSchema,
99
96
  target,
100
- handler
97
+ handlers,
98
+ true
101
99
  )
102
100
  })
103
101
  )
104
102
  }
105
103
  }
106
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
104
+ if (!noCollect) {
105
+ handlers.collect({ path, value, typeSchema, fieldSchema, target })
106
+ }
107
107
  }
package/src/set/rest.ts CHANGED
@@ -25,14 +25,15 @@ export const boolean: Parser<'boolean'> = async (
25
25
  fieldSchema,
26
26
  typeSchema,
27
27
  target,
28
- handlers
28
+ handlers,
29
+ noCollect
29
30
  ) => {
30
- // value .default
31
- // $increment / $decrement
32
31
  if (typeof value !== 'boolean') {
33
32
  error(path, ParseError.incorrectFormat)
34
33
  }
35
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
34
+ if (!noCollect) {
35
+ handlers.collect({ path, value, typeSchema, fieldSchema, target })
36
+ }
36
37
  }
37
38
 
38
39
  export const enumParser: Parser<'enum'> = async (
@@ -41,12 +42,15 @@ export const enumParser: Parser<'enum'> = async (
41
42
  fieldSchema,
42
43
  typeSchema,
43
44
  target,
44
- handlers
45
+ handlers,
46
+ noCollect
45
47
  ) => {
46
48
  const enumValues = fieldSchema.enum
47
49
  for (let i = 0; i < enumValues.length; i++) {
48
50
  if (deepEqual(enumValues[i], value)) {
49
- handlers.collect({ path, value: i, typeSchema, fieldSchema, target })
51
+ if (!noCollect) {
52
+ handlers.collect({ path, value: i, typeSchema, fieldSchema, target })
53
+ }
50
54
  return
51
55
  }
52
56
  }
@@ -59,17 +63,20 @@ export const json: Parser<'json'> = async (
59
63
  fieldSchema,
60
64
  typeSchema,
61
65
  target,
62
- handlers
66
+ handlers,
67
+ noCollect
63
68
  ) => {
64
69
  try {
65
70
  const parsedValue = JSON.stringify(value)
66
- handlers.collect({
67
- path,
68
- value: parsedValue,
69
- typeSchema,
70
- fieldSchema,
71
- target,
72
- })
71
+ if (!noCollect) {
72
+ handlers.collect({
73
+ path,
74
+ value: parsedValue,
75
+ typeSchema,
76
+ fieldSchema,
77
+ target,
78
+ })
79
+ }
73
80
  } catch (err) {
74
81
  throw err(path, ParseError.incorrectFormat)
75
82
  }
package/src/set/string.ts CHANGED
@@ -101,10 +101,13 @@ export const string: Parser<'string'> = async (
101
101
  fieldSchema,
102
102
  typeSchema,
103
103
  target,
104
- handlers
104
+ handlers,
105
+ noCollect
105
106
  ) => {
106
107
  validate(path, value, fieldSchema)
107
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
108
+ if (!noCollect) {
109
+ handlers.collect({ path, value, typeSchema, fieldSchema, target })
110
+ }
108
111
  }
109
112
 
110
113
  export const text: Parser<'text'> = async (
@@ -113,18 +116,21 @@ export const text: Parser<'text'> = async (
113
116
  fieldSchema,
114
117
  typeSchema,
115
118
  target,
116
- handlers
119
+ handlers,
120
+ noCollect
117
121
  ) => {
118
122
  const valueType = typeof value
119
123
  if (target.$language && valueType === 'string') {
120
124
  validate(path, value, fieldSchema)
121
- handlers.collect({
122
- path,
123
- value: { [target.$language]: value },
124
- typeSchema,
125
- fieldSchema,
126
- target,
127
- })
125
+ if (!noCollect) {
126
+ handlers.collect({
127
+ path,
128
+ value: { [target.$language]: value },
129
+ typeSchema,
130
+ fieldSchema,
131
+ target,
132
+ })
133
+ }
128
134
  return
129
135
  }
130
136
 
@@ -135,25 +141,44 @@ export const text: Parser<'text'> = async (
135
141
  for (const key in value) {
136
142
  const newPath = [...path, key]
137
143
 
138
- if (typeof value[key] === 'object' && value[key].$delete === true) {
144
+ if (typeof value[key] === 'object') {
145
+ if (value[key].$value) {
146
+ text(
147
+ [...path, key, '$value'],
148
+ value[key].$value,
149
+ fieldSchema,
150
+ typeSchema,
151
+ target,
152
+ handlers,
153
+ true
154
+ )
155
+ }
156
+
157
+ // if (value[key].$default) {
158
+ // }
159
+
160
+ if (!noCollect) {
161
+ handlers.collect({
162
+ path: newPath,
163
+ value: null,
164
+ typeSchema,
165
+ fieldSchema,
166
+ target,
167
+ })
168
+ }
169
+ continue
170
+ }
171
+
172
+ validate(newPath, value[key], fieldSchema)
173
+
174
+ if (!noCollect) {
139
175
  handlers.collect({
140
176
  path: newPath,
141
- value: null,
177
+ value: value[key],
142
178
  typeSchema,
143
179
  fieldSchema,
144
180
  target,
145
181
  })
146
- continue
147
182
  }
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
183
  }
159
184
  }
package/src/set/types.ts CHANGED
@@ -11,7 +11,8 @@ export type Parser<K extends keyof BasedSchemaFields> = (
11
11
  fieldSchema: BasedSchemaFields[K],
12
12
  typeSchema: BasedSchemaType,
13
13
  target: BasedSetTarget,
14
- handlers: BasedSetHandlers
14
+ handlers: BasedSetHandlers,
15
+ noCollect?: boolean
15
16
  ) => Promise<void>
16
17
 
17
18
  export type Parsers = {
package/test/setWalker.ts CHANGED
@@ -9,6 +9,9 @@ const schema: BasedSchema = {
9
9
  visits: {
10
10
  type: 'cardinality',
11
11
  },
12
+ blub: {
13
+ type: 'number',
14
+ },
12
15
  snurp: {
13
16
  type: 'array',
14
17
  values: {
@@ -149,6 +152,9 @@ test.serial('collect correctly', async (t) => {
149
152
  snurp: false,
150
153
  ua: '123435',
151
154
  },
155
+ blub: {
156
+ $increment: 1,
157
+ },
152
158
  bla: false,
153
159
  time: now, // do more later
154
160
  setje: [1, 2, 3],
@@ -203,6 +209,7 @@ test.serial('collect correctly', async (t) => {
203
209
 
204
210
  const result = [
205
211
  { path: ['visits'], value: '3a9009740ee' },
212
+ { path: ['blub'], value: { $increment: 1 } },
206
213
  { path: ['bla'], value: false },
207
214
  { path: ['time'], value: now },
208
215
  { path: ['form', 'lastName'], value: 'de beer' },
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"}