@based/schema 0.0.9 → 0.0.11

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 (97) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +1 -0
  3. package/dist/index.js.map +1 -1
  4. package/dist/set/collections.js +52 -19
  5. package/dist/set/collections.js.map +1 -1
  6. package/dist/set/error.d.ts +5 -2
  7. package/dist/set/error.js +10 -3
  8. package/dist/set/error.js.map +1 -1
  9. package/dist/set/index.d.ts +2 -2
  10. package/dist/set/index.js +67 -12
  11. package/dist/set/index.js.map +1 -1
  12. package/dist/set/number.js +37 -23
  13. package/dist/set/number.js.map +1 -1
  14. package/dist/set/parseDefaultAndValue.js +5 -3
  15. package/dist/set/parseDefaultAndValue.js.map +1 -1
  16. package/dist/set/references.js +12 -5
  17. package/dist/set/references.js.map +1 -1
  18. package/dist/set/rest.js +4 -4
  19. package/dist/set/rest.js.map +1 -1
  20. package/dist/set/string.js +48 -31
  21. package/dist/set/string.js.map +1 -1
  22. package/dist/set2/index.js +1 -0
  23. package/dist/set2/index.js.map +1 -0
  24. package/dist/types.d.ts +10 -2
  25. package/dist/types.js.map +1 -1
  26. package/dist/updateSchema.d.ts +2 -0
  27. package/dist/updateSchema.js +16 -0
  28. package/dist/updateSchema.js.map +1 -0
  29. package/dist/validateSchema.js +5 -1
  30. package/dist/validateSchema.js.map +1 -1
  31. package/dist/walker.d.ts +50 -0
  32. package/dist/walker.js +83 -0
  33. package/dist/walker.js.map +1 -0
  34. package/package.json +1 -1
  35. package/src/index.ts +2 -0
  36. package/src/set/collections.ts +56 -21
  37. package/src/set/error.ts +14 -3
  38. package/src/set/index.ts +87 -12
  39. package/src/set/number.ts +39 -22
  40. package/src/set/parseDefaultAndValue.ts +13 -3
  41. package/src/set/references.ts +11 -5
  42. package/src/set/rest.ts +4 -4
  43. package/src/set/string.ts +78 -35
  44. package/src/set2/index.ts +0 -0
  45. package/src/types.ts +18 -11
  46. package/src/updateSchema.ts +18 -0
  47. package/src/validateSchema.ts +6 -1
  48. package/src/walker.ts +152 -0
  49. package/test/number.ts +646 -0
  50. package/test/reference.ts +207 -0
  51. package/test/setWalker.ts +223 -53
  52. package/test/string.ts +183 -0
  53. package/test/text.ts +171 -0
  54. package/test/validateSchema.ts +0 -1
  55. package/test/walker.ts +99 -0
  56. package/dist/deepPartial.js +0 -3
  57. package/dist/deepPartial.js.map +0 -1
  58. package/dist/schema.d.ts +0 -1
  59. package/dist/schema.js +0 -3
  60. package/dist/schema.js.map +0 -1
  61. package/dist/set/checkDefaultAndValue.d.ts +0 -3
  62. package/dist/set/checkDefaultAndValue.js +0 -33
  63. package/dist/set/checkDefaultAndValue.js.map +0 -1
  64. package/dist/set/enum.d.ts +0 -2
  65. package/dist/set/enum.js +0 -15
  66. package/dist/set/enum.js.map +0 -1
  67. package/dist/set/fieldValidator.d.ts +0 -6
  68. package/dist/set/fieldValidator.js +0 -144
  69. package/dist/set/fieldValidator.js.map +0 -1
  70. package/dist/set/handleError.d.ts +0 -1
  71. package/dist/set/handleError.js +0 -9
  72. package/dist/set/handleError.js.map +0 -1
  73. package/dist/set/number copy.d.ts +0 -4
  74. package/dist/set/number copy.js +0 -57
  75. package/dist/set/number copy.js.map +0 -1
  76. package/dist/set/rest copy.d.ts +0 -5
  77. package/dist/set/rest copy.js +0 -53
  78. package/dist/set/rest copy.js.map +0 -1
  79. package/dist/setWalker.d.ts +0 -11
  80. package/dist/setWalker.js +0 -189
  81. package/dist/setWalker.js.map +0 -1
  82. package/dist/transformers.d.ts +0 -3
  83. package/dist/transformers.js +0 -18
  84. package/dist/transformers.js.map +0 -1
  85. package/dist/typeWalker.d.ts +0 -3
  86. package/dist/typeWalker.js +0 -18
  87. package/dist/typeWalker.js.map +0 -1
  88. package/dist/validate.d.ts +0 -4
  89. package/dist/validate.js +0 -34
  90. package/dist/validate.js.map +0 -1
  91. package/dist/validateFields.d.ts +0 -4
  92. package/dist/validateFields.js +0 -34
  93. package/dist/validateFields.js.map +0 -1
  94. package/dist/validateSchema copy.d.ts +0 -4
  95. package/dist/validateSchema copy.js +0 -34
  96. package/dist/validateSchema copy.js.map +0 -1
  97. /package/dist/{deepPartial.d.ts → set2/index.d.ts} +0 -0
package/src/set/string.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  import { Parser } from './types'
2
2
  import { error, ParseError } from './error'
3
- import { BasedSchemaFieldString, BasedSchemaFieldText } from '../types'
3
+ import {
4
+ BasedSchemaFieldString,
5
+ BasedSchemaFieldText,
6
+ BasedSchemaLanguage,
7
+ BasedSetHandlers,
8
+ } from '../types'
4
9
  import validators from 'validator'
5
10
  import { parseValueAndDefault } from './parseDefaultAndValue'
6
11
 
@@ -72,28 +77,30 @@ const formatPatterns: Record<
72
77
  }
73
78
 
74
79
  const validate = (
80
+ handlers: BasedSetHandlers,
75
81
  path: (string | number)[],
76
82
  value: string,
77
83
  fieldSchema: BasedSchemaFieldText | BasedSchemaFieldString
78
84
  ) => {
79
85
  if (typeof value !== 'string') {
80
- error(path, ParseError.incorrectFormat)
86
+ error(handlers, ParseError.incorrectFormat, path)
81
87
  }
82
88
  if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
83
- error(path, ParseError.subceedsMinimum)
89
+ error(handlers, ParseError.subceedsMinimum, path)
84
90
  }
85
91
  if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
86
- error(path, ParseError.exceedsMaximum)
92
+ error(handlers, ParseError.exceedsMaximum, path)
87
93
  }
88
94
  if (fieldSchema.pattern) {
89
95
  const re = new RegExp(fieldSchema.pattern)
90
96
  if (!re.test(value)) {
91
- error(path, ParseError.incorrectFormat)
97
+ error(handlers, ParseError.incorrectFormat, path)
92
98
  }
93
99
  }
94
100
  if (fieldSchema.format && !formatPatterns[fieldSchema.format](value)) {
95
- error(path, ParseError.incorrectFormat)
101
+ error(handlers, ParseError.incorrectFormat, path)
96
102
  }
103
+ // return true / false and add collectError
97
104
  }
98
105
 
99
106
  export const string: Parser<'string'> = async (
@@ -118,7 +125,7 @@ export const string: Parser<'string'> = async (
118
125
  ) {
119
126
  return
120
127
  }
121
- validate(path, value, fieldSchema)
128
+ validate(handlers, path, value, fieldSchema)
122
129
  if (!noCollect) {
123
130
  handlers.collect({ path, value, typeSchema, fieldSchema, target })
124
131
  }
@@ -135,7 +142,7 @@ export const text: Parser<'text'> = async (
135
142
  ) => {
136
143
  const valueType = typeof value
137
144
  if (target.$language && valueType === 'string') {
138
- validate(path, value, fieldSchema)
145
+ validate(handlers, path, value, fieldSchema)
139
146
  if (!noCollect) {
140
147
  handlers.collect({
141
148
  path,
@@ -149,7 +156,7 @@ export const text: Parser<'text'> = async (
149
156
  }
150
157
 
151
158
  if (valueType !== 'object') {
152
- error(path, ParseError.incorrectFormat)
159
+ error(handlers, ParseError.incorrectFormat, path)
153
160
  }
154
161
 
155
162
  if (
@@ -161,51 +168,87 @@ export const text: Parser<'text'> = async (
161
168
  typeSchema,
162
169
  target,
163
170
  handlers,
164
- noCollect
171
+ true
165
172
  ))
166
173
  ) {
174
+ if (!noCollect) {
175
+ handlers.collect({
176
+ path,
177
+ value: {
178
+ [target.$language]: value,
179
+ },
180
+ typeSchema,
181
+ fieldSchema,
182
+ target,
183
+ })
184
+ }
185
+ return
186
+ } else if (
187
+ await parseValueAndDefault(
188
+ path,
189
+ value,
190
+ fieldSchema,
191
+ typeSchema,
192
+ target,
193
+ handlers,
194
+ true
195
+ )
196
+ ) {
197
+ if (!noCollect) {
198
+ handlers.collect({
199
+ path,
200
+ value,
201
+ typeSchema,
202
+ fieldSchema,
203
+ target,
204
+ })
205
+ }
167
206
  return
168
207
  }
169
208
 
170
209
  for (const key in value) {
171
- const newPath = [...path, key]
210
+ if (!target.schema.languages.includes(<BasedSchemaLanguage>key)) {
211
+ error(handlers, ParseError.languageNotSupported, path)
212
+ }
172
213
 
173
214
  if (typeof value[key] === 'object') {
174
- if (value[key].$value) {
175
- text(
176
- [...path, key, '$value'],
177
- value[key].$value,
215
+ if (
216
+ await parseValueAndDefault(
217
+ path,
218
+ value[key],
178
219
  fieldSchema,
179
220
  typeSchema,
180
221
  target,
181
222
  handlers,
182
223
  true
183
224
  )
225
+ ) {
226
+ continue
184
227
  }
185
- if (!noCollect) {
186
- handlers.collect({
187
- path: newPath,
188
- value: null,
189
- typeSchema,
190
- fieldSchema,
191
- target,
192
- })
193
- }
194
- continue
195
228
  }
196
229
 
197
- // if
198
-
199
- // validate(newPath, value[key], fieldSchema)
200
-
201
- if (!noCollect) {
202
- handlers.collect({
203
- path: newPath,
204
- value: value[key],
205
- typeSchema,
230
+ if (
231
+ !(await parseValueAndDefault(
232
+ path,
233
+ { [key]: value[key] },
206
234
  fieldSchema,
235
+ typeSchema,
207
236
  target,
208
- })
237
+ handlers,
238
+ true
239
+ ))
240
+ ) {
241
+ validate(handlers, path, value[key], fieldSchema)
209
242
  }
210
243
  }
244
+
245
+ if (!noCollect) {
246
+ handlers.collect({
247
+ path,
248
+ value,
249
+ typeSchema,
250
+ fieldSchema,
251
+ target,
252
+ })
253
+ }
211
254
  }
File without changes
package/src/types.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Language } from './languages'
2
- import type { PartialDeep } from 'type-fest'
2
+ import type { PartialDeep, SetOptional } from 'type-fest'
3
+ import { ParseError } from './set/error'
3
4
 
4
5
  // Schema type
5
6
  // inspiration from https://json-schema.org/understanding-json-schema/index.html
@@ -54,7 +55,11 @@ export type BasedSchemaContentMediaType =
54
55
  | 'image/png'
55
56
  | 'image/jpeg'
56
57
  | 'video/mp4'
57
- | string
58
+ | 'image/*'
59
+ | 'video/*'
60
+ | 'audio/*'
61
+ | '*/*'
62
+ | `${string}/${string}`
58
63
 
59
64
  export type BasedSchemaFieldShared = {
60
65
  hooks?:
@@ -83,13 +88,12 @@ export type BasedSchemaFieldShared = {
83
88
  }
84
89
 
85
90
  // -------------- Primitive ---------------
86
-
87
91
  export type BasedSchemaStringShared = {
88
92
  minLength?: number
89
93
  maxLength?: number
90
94
  contentMediaEncoding?: string // base64
91
- contentMediaType?: BasedSchemaContentMediaType
92
- pattern?: BasedSchemaPattern
95
+ contentMediaType?: BasedSchemaContentMediaType // 'image/*'
96
+ pattern?: BasedSchemaPattern // TODO: does not exist
93
97
  format?:
94
98
  | 'email'
95
99
  | 'URL'
@@ -340,6 +344,7 @@ export type BasedSetTarget = {
340
344
  $id?: string
341
345
  schema: BasedSchema
342
346
  $language?: BasedSchemaLanguage
347
+ required: (number | string)[][]
343
348
  }
344
349
 
345
350
  export type BasedSchemaCollectProps = {
@@ -351,17 +356,19 @@ export type BasedSchemaCollectProps = {
351
356
  }
352
357
 
353
358
  export type BasedSetHandlers = {
359
+ collectErrors: (props: { message: string; code: ParseError }) => void
360
+
354
361
  collect: (props: BasedSchemaCollectProps) => void
355
362
 
356
- // has to be fixed / decided upon
357
- // ['bla[0].xxx', '']
358
- // [bla, [0]', xxx]
359
- // (number|string)[]
360
- // typeschema, target) => Promise<boolean>
363
+ checkRequiredFields: (path: (string | number)[]) => Promise<boolean>
361
364
 
362
- // $filter
363
365
  referenceFilterCondition: (
364
366
  referenceId: string,
365
367
  $filter: any
366
368
  ) => Promise<boolean>
367
369
  }
370
+
371
+ export type BasedSetOptionalHandlers = SetOptional<
372
+ BasedSetHandlers,
373
+ 'collectErrors' | 'collect'
374
+ >
@@ -0,0 +1,18 @@
1
+ import { BasedSchema, BasedSchemaPartial } from './types'
2
+
3
+ export const updateSchema = async (
4
+ newSchema: BasedSchemaPartial,
5
+ oldSchema: BasedSchema = {
6
+ $defs: {},
7
+ types: {},
8
+ languages: ['en'],
9
+ root: { fields: {} },
10
+ prefixToTypeMapping: {},
11
+ }
12
+ ): Promise<BasedSchema> => {
13
+ // add sha
14
+
15
+ // put isRequired on the new schema in REQUIRED arrays
16
+
17
+ return oldSchema
18
+ }
@@ -5,6 +5,8 @@ import {
5
5
  BasedSchemaField,
6
6
  } from './types'
7
7
 
8
+ // gaurd in the schema for refs in arrays
9
+
8
10
  export const validateType = (
9
11
  fromSchema: BasedSchemaPartial,
10
12
  typeName: string,
@@ -24,7 +26,9 @@ export const validateField = (
24
26
  fromSchema: BasedSchemaPartial,
25
27
  path: string[],
26
28
  field: BasedSchemaFieldPartial
27
- ) => {}
29
+ ) => {
30
+ //
31
+ }
28
32
 
29
33
  export const validateSchema = (
30
34
  schema: BasedSchemaPartial
@@ -44,6 +48,7 @@ export const validateSchema = (
44
48
  }
45
49
 
46
50
  if (schema.root) {
51
+ validateType(schema, 'root', schema.root)
47
52
  }
48
53
 
49
54
  if (schema.types) {
package/src/walker.ts ADDED
@@ -0,0 +1,152 @@
1
+ import { ParseError } from './set/error'
2
+ import { BasedSchema, BasedSetHandlers, BasedSetTarget } from './types'
3
+ import { BasedSchemaType, BasedSchemaFields } from './types'
4
+
5
+ type Path = (string | number)[]
6
+
7
+ type ErrorHandler<T> = (args: Args<T>, code: ParseError) => void
8
+
9
+ type Parse<T> = (
10
+ args: Args<T>,
11
+ key?: string | number,
12
+ value?: any
13
+ ) => Promise<Args<T> | void> // If true will not continue
14
+
15
+ export type Args<
16
+ T,
17
+ K extends keyof BasedSchemaFields = keyof BasedSchemaFields
18
+ > = {
19
+ schema: BasedSchema
20
+ parentValue?: any
21
+ skipCollection?: boolean
22
+ fieldSchema?: BasedSchemaFields[K]
23
+ typeSchema?: BasedSchemaType
24
+ path: Path
25
+ value: any
26
+ target: T
27
+ parse: Parse<T>
28
+ collect: (args: Args<T>) => any
29
+ backtrack: (collectedCommands: any[]) => any
30
+ requiresAsyncValidaton: (validationType: any) => Promise<any>
31
+ error: ErrorHandler<T>
32
+ }
33
+
34
+ export type FieldParser<T, K extends keyof BasedSchemaFields> = (
35
+ args: Args<T, K>
36
+ ) => Promise<Args<T> | void>
37
+
38
+ export type KeyParser<T> = (
39
+ args: Args<T, keyof BasedSchemaFields>
40
+ ) => Promise<Args<T> | void>
41
+
42
+ export type Opts<T> = {
43
+ schema: BasedSchema
44
+ init: (
45
+ value: any,
46
+ opts: Opts<T>,
47
+ errors: { code: ParseError; message: string }[]
48
+ ) => Promise<T>
49
+ parsers: {
50
+ fields: Partial<{
51
+ [Key in keyof BasedSchemaFields]: FieldParser<T, Key>
52
+ }>
53
+ keys: { [key: string]: KeyParser<T> } // $list -> true
54
+ any: KeyParser<T> // y.x
55
+ }
56
+ collect: (args: Args<T>) => any
57
+ backtrack: (collectedCommands: any[]) => any // from back TRACKS OR COLLECT
58
+ requiresAsyncValidaton: (validationType: any) => Promise<boolean>
59
+ }
60
+
61
+ export const walk = async <T>(
62
+ opts: Opts<T>,
63
+ value: any
64
+ ): Promise<{
65
+ target: T
66
+ errors: { code: ParseError; message: string }[]
67
+ }> => {
68
+ const errors: { code: ParseError; message: string }[] = []
69
+ const target = await opts.init(value, opts, errors)
70
+
71
+ const errorsCollector: ErrorHandler<T> = (args, code) => {
72
+ errors.push({
73
+ code,
74
+ message: 'flap flap',
75
+ })
76
+ }
77
+
78
+ const parse: Parse<T> = async (prevArgs, key, value) => {
79
+ const collectedCommands: any[] = []
80
+ const fromBackTrack: any[] = []
81
+ const args: Args<T> = {
82
+ schema: opts.schema,
83
+ path: key ? [...prevArgs.path, key] : prevArgs.path,
84
+ parentValue: value ? prevArgs.value : undefined,
85
+ value: value ?? prevArgs.value,
86
+ target,
87
+ parse: prevArgs.parse,
88
+ collect: (args) => {
89
+ collectedCommands.push(opts.collect(args))
90
+ },
91
+ backtrack: (args) => {
92
+ fromBackTrack.push(opts.backtrack(args))
93
+ },
94
+ error: errorsCollector,
95
+ requiresAsyncValidaton: opts.requiresAsyncValidaton,
96
+ }
97
+ if (typeof args.value === 'object' && args.value !== null) {
98
+ const q: Promise<Args<T> | void>[] = []
99
+ if (Array.isArray(args.value)) {
100
+ for (let i = 0; i < args.value.length; i++) {
101
+ //
102
+ const parser = opts.parsers.keys[i] || opts.parsers.any
103
+ q.push(
104
+ parser({ ...args, value: args.value[i], path: [...args.path, i] })
105
+ )
106
+ }
107
+ } else {
108
+ for (const key in args.value) {
109
+ const parser = opts.parsers.keys[key] || opts.parsers.any
110
+ q.push(
111
+ (async () => {
112
+ const newArgs = await parser({
113
+ ...args,
114
+ value: args.value[key],
115
+ path: [...args.path, key],
116
+ })
117
+
118
+ if (newArgs) {
119
+ return parse(newArgs)
120
+ }
121
+ })()
122
+ )
123
+ }
124
+ }
125
+ await Promise.all(q)
126
+ if (fromBackTrack.length) {
127
+ opts.backtrack(fromBackTrack)
128
+ } else if (collectedCommands.length) {
129
+ opts.backtrack(collectedCommands)
130
+ }
131
+ }
132
+ }
133
+
134
+ const args: Args<T> = {
135
+ schema: opts.schema,
136
+ path: [],
137
+ value,
138
+ target,
139
+ parse,
140
+ collect: opts.collect,
141
+ backtrack: opts.backtrack,
142
+ error: errorsCollector,
143
+ requiresAsyncValidaton: opts.requiresAsyncValidaton,
144
+ }
145
+
146
+ parse(args)
147
+
148
+ return {
149
+ target,
150
+ errors,
151
+ }
152
+ }