@based/schema 0.0.10 → 0.0.12

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 (90) 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 +39 -17
  5. package/dist/set/collections.js.map +1 -1
  6. package/dist/set/error.d.ts +4 -2
  7. package/dist/set/error.js +9 -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 +48 -8
  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 +38 -34
  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 +8 -2
  25. package/dist/types.js.map +1 -1
  26. package/dist/walker.d.ts +50 -0
  27. package/dist/walker.js +83 -0
  28. package/dist/walker.js.map +1 -0
  29. package/package.json +1 -1
  30. package/src/index.ts +2 -0
  31. package/src/set/collections.ts +42 -17
  32. package/src/set/error.ts +13 -3
  33. package/src/set/index.ts +65 -8
  34. package/src/set/number.ts +39 -22
  35. package/src/set/parseDefaultAndValue.ts +13 -3
  36. package/src/set/references.ts +11 -5
  37. package/src/set/rest.ts +4 -4
  38. package/src/set/string.ts +48 -36
  39. package/src/set2/index.ts +0 -0
  40. package/src/types.ts +16 -7
  41. package/src/walker.ts +152 -0
  42. package/test/number.ts +646 -0
  43. package/test/reference.ts +207 -0
  44. package/test/setWalker.ts +101 -21
  45. package/test/string.ts +183 -0
  46. package/test/text.ts +171 -0
  47. package/test/validateSchema.ts +0 -1
  48. package/test/walker.ts +99 -0
  49. package/dist/deepPartial.js +0 -3
  50. package/dist/deepPartial.js.map +0 -1
  51. package/dist/schema.d.ts +0 -1
  52. package/dist/schema.js +0 -3
  53. package/dist/schema.js.map +0 -1
  54. package/dist/set/checkDefaultAndValue.d.ts +0 -3
  55. package/dist/set/checkDefaultAndValue.js +0 -33
  56. package/dist/set/checkDefaultAndValue.js.map +0 -1
  57. package/dist/set/enum.d.ts +0 -2
  58. package/dist/set/enum.js +0 -15
  59. package/dist/set/enum.js.map +0 -1
  60. package/dist/set/fieldValidator.d.ts +0 -6
  61. package/dist/set/fieldValidator.js +0 -144
  62. package/dist/set/fieldValidator.js.map +0 -1
  63. package/dist/set/handleError.d.ts +0 -1
  64. package/dist/set/handleError.js +0 -9
  65. package/dist/set/handleError.js.map +0 -1
  66. package/dist/set/number copy.d.ts +0 -4
  67. package/dist/set/number copy.js +0 -57
  68. package/dist/set/number copy.js.map +0 -1
  69. package/dist/set/rest copy.d.ts +0 -5
  70. package/dist/set/rest copy.js +0 -53
  71. package/dist/set/rest copy.js.map +0 -1
  72. package/dist/setWalker.d.ts +0 -11
  73. package/dist/setWalker.js +0 -189
  74. package/dist/setWalker.js.map +0 -1
  75. package/dist/transformers.d.ts +0 -3
  76. package/dist/transformers.js +0 -18
  77. package/dist/transformers.js.map +0 -1
  78. package/dist/typeWalker.d.ts +0 -3
  79. package/dist/typeWalker.js +0 -18
  80. package/dist/typeWalker.js.map +0 -1
  81. package/dist/validate.d.ts +0 -4
  82. package/dist/validate.js +0 -34
  83. package/dist/validate.js.map +0 -1
  84. package/dist/validateFields.d.ts +0 -4
  85. package/dist/validateFields.js +0 -34
  86. package/dist/validateFields.js.map +0 -1
  87. package/dist/validateSchema copy.d.ts +0 -4
  88. package/dist/validateSchema copy.js +0 -34
  89. package/dist/validateSchema copy.js.map +0 -1
  90. /package/dist/{deepPartial.d.ts → set2/index.d.ts} +0 -0
package/src/set/index.ts CHANGED
@@ -4,9 +4,12 @@ import {
4
4
  BasedSetHandlers,
5
5
  BasedSchema,
6
6
  BasedSetTarget,
7
+ BasedSchemaCollectProps,
8
+ BasedSetOptionalHandlers,
7
9
  } from '../types'
8
10
  import { error, ParseError } from './error'
9
11
  import parsers from './parsers'
12
+ import { SetOptional } from 'type-fest'
10
13
 
11
14
  export const fieldWalker = async (
12
15
  path: (string | number)[],
@@ -34,13 +37,13 @@ export const fieldWalker = async (
34
37
  const typeDef = fieldSchema.type ?? ('enum' in fieldSchema ? 'enum' : '')
35
38
 
36
39
  if (!typeDef) {
37
- error(path, ParseError.fieldDoesNotExist)
40
+ error(handlers, ParseError.fieldDoesNotExist, path)
38
41
  }
39
42
 
40
43
  if ('customValidator' in fieldSchema) {
41
44
  const customValidator = fieldSchema.customValidator
42
45
  if (!(await customValidator(value, path, target))) {
43
- error(path, ParseError.incorrectFormat)
46
+ error(handlers, ParseError.incorrectFormat, path)
44
47
  }
45
48
  }
46
49
 
@@ -54,20 +57,51 @@ export const fieldWalker = async (
54
57
  export const setWalker = async (
55
58
  schema: BasedSchema,
56
59
  value: { [key: string]: any },
57
- handlers: BasedSetHandlers
60
+ inHandlers: BasedSetOptionalHandlers
58
61
  ): Promise<BasedSetTarget> => {
62
+ let errors: {
63
+ message: string
64
+ code: ParseError
65
+ }[]
66
+
67
+ const collect: BasedSchemaCollectProps[] = []
68
+
69
+ const x = { ...inHandlers }
70
+
71
+ if (!('collectErrors' in x)) {
72
+ errors = []
73
+ x.collectErrors = (info) => {
74
+ errors.push(info)
75
+ }
76
+ }
77
+
78
+ let prevCollect: any
79
+
80
+ if (!('collect' in x)) {
81
+ x.collect = (info) => {
82
+ collect.push(info)
83
+ }
84
+ } else {
85
+ prevCollect = x.collect
86
+ x.collect = (info) => {
87
+ collect.push(info)
88
+ }
89
+ }
90
+
91
+ const handlers: BasedSetHandlers = <BasedSetHandlers>x
92
+
59
93
  let type: string
60
94
 
61
95
  if (value.$id) {
62
96
  type = schema.prefixToTypeMapping[value.$id.slice(0, 2)]
63
97
  if (!type) {
64
- error([value.$id], ParseError.incorrectNodeType)
98
+ error(handlers, ParseError.incorrectNodeType, [value.$id])
65
99
  }
66
100
  }
67
101
 
68
102
  if (value.type) {
69
103
  if (type && value.type !== type) {
70
- error([value.$id, value.type], ParseError.incorrectNodeType)
104
+ error(handlers, ParseError.incorrectNodeType, [value.$id, value.type])
71
105
  }
72
106
  type = value.type
73
107
  }
@@ -75,7 +109,7 @@ export const setWalker = async (
75
109
  const schemaType = schema.types[type]
76
110
 
77
111
  if (!schemaType) {
78
- error([type], ParseError.incorrectNodeType)
112
+ error(handlers, ParseError.incorrectNodeType, [type])
79
113
  }
80
114
 
81
115
  const target: BasedSetTarget = {
@@ -84,6 +118,13 @@ export const setWalker = async (
84
118
  required: [],
85
119
  }
86
120
 
121
+ if (value.$language) {
122
+ if (!schema.languages.includes(value.$language)) {
123
+ error(handlers, ParseError.languageNotSupported, ['$language'])
124
+ }
125
+ target.$language = value.$language
126
+ }
127
+
87
128
  if (value.$id) {
88
129
  target.$id = value.$id
89
130
  } else if (value.$alias) {
@@ -96,7 +137,7 @@ export const setWalker = async (
96
137
  if (key[0] !== '$' && key !== 'type') {
97
138
  const fieldSchema = schemaType.fields[key]
98
139
  if (!fieldSchema) {
99
- error([key], ParseError.fieldDoesNotExist)
140
+ error(handlers, ParseError.fieldDoesNotExist, [key])
100
141
  } else {
101
142
  q.push(
102
143
  fieldWalker(
@@ -131,10 +172,26 @@ export const setWalker = async (
131
172
  for (let i = 0; i < requireDefined.length; i++) {
132
173
  if (!requireDefined[i]) {
133
174
  const r = target.required[i]
134
- error(r, ParseError.requiredFieldNotDefined)
175
+ error(handlers, ParseError.requiredFieldNotDefined, r)
135
176
  }
136
177
  }
137
178
  }
138
179
 
180
+ if (errors?.length) {
181
+ const err = new Error(
182
+ 'Errors in in set' +
183
+ errors.reduce((str, info) => {
184
+ return str + `\n - ${info.message}`
185
+ }, '')
186
+ )
187
+ throw err
188
+ }
189
+
190
+ if (prevCollect) {
191
+ for (const x of collect) {
192
+ prevCollect(x)
193
+ }
194
+ }
195
+
139
196
  return target
140
197
  }
package/src/set/number.ts CHANGED
@@ -4,21 +4,25 @@ import {
4
4
  BasedSchemaFieldInteger,
5
5
  BasedSchemaFieldNumber,
6
6
  BasedSchemaFieldTimeStamp,
7
+ BasedSetHandlers,
7
8
  } from '../types'
8
9
 
9
10
  const validate = (
11
+ handlers: BasedSetHandlers,
10
12
  path: (number | string)[],
11
13
  value: any,
12
14
  fieldSchema:
13
15
  | BasedSchemaFieldInteger
14
16
  | BasedSchemaFieldNumber
15
17
  | BasedSchemaFieldTimeStamp
16
- ): number => {
18
+ ): number | false => {
17
19
  if (typeof value !== 'number') {
18
- error(path, ParseError.incorrectFormat)
20
+ error(handlers, ParseError.incorrectFormat, path)
21
+ return false
19
22
  }
20
23
  if (fieldSchema.type === 'integer' && value - Math.floor(value) !== 0) {
21
- error(path, ParseError.incorrectFormat)
24
+ error(handlers, ParseError.incorrectFormat, path)
25
+ return false
22
26
  }
23
27
  if (
24
28
  fieldSchema.multipleOf &&
@@ -26,26 +30,36 @@ const validate = (
26
30
  Math.floor(value / fieldSchema.multipleOf) !==
27
31
  0
28
32
  ) {
29
- error(path, ParseError.incorrectFormat)
33
+ error(handlers, ParseError.incorrectFormat, path)
34
+ return false
30
35
  }
31
36
  if (fieldSchema.maximum) {
32
- if (fieldSchema.exclusiveMaximum && value > value) {
33
- error(path, ParseError.exceedsMaximum)
34
- } else if (value >= value) {
35
- error(path, ParseError.exceedsMaximum)
37
+ if (fieldSchema.exclusiveMaximum) {
38
+ if (value >= fieldSchema.maximum) {
39
+ error(handlers, ParseError.exceedsMaximum, path)
40
+ return false
41
+ }
42
+ } else if (value > fieldSchema.maximum) {
43
+ error(handlers, ParseError.exceedsMaximum, path)
44
+ return false
36
45
  }
37
46
  }
38
47
  if (fieldSchema.minimum) {
39
- if (fieldSchema.exclusiveMinimum && value < value) {
40
- error(path, ParseError.subceedsMinimum)
41
- } else if (value <= value) {
42
- error(path, ParseError.subceedsMinimum)
48
+ if (fieldSchema.exclusiveMinimum) {
49
+ if (value <= fieldSchema.minimum) {
50
+ error(handlers, ParseError.subceedsMinimum, path)
51
+ return false
52
+ }
53
+ } else if (value < fieldSchema.minimum) {
54
+ error(handlers, ParseError.subceedsMinimum, path)
55
+ return false
43
56
  }
44
57
  }
45
58
  return value
46
59
  }
47
60
 
48
61
  const shared = (
62
+ handlers: BasedSetHandlers,
49
63
  path: (number | string)[],
50
64
  value: any,
51
65
  fieldSchema:
@@ -55,22 +69,23 @@ const shared = (
55
69
  ): any => {
56
70
  if (typeof value === 'object') {
57
71
  if (value.$increment) {
58
- validate([...path, '$increment'], value.$increment, fieldSchema)
72
+ validate(handlers, [...path, '$increment'], value.$increment, fieldSchema)
59
73
  }
60
74
  if (value.$decrement) {
61
- validate([...path, '$decrement'], value.$decrement, fieldSchema)
75
+ validate(handlers, [...path, '$decrement'], value.$decrement, fieldSchema)
62
76
  }
63
77
  if (value.$value !== undefined) {
64
- validate(path, value.$value, fieldSchema)
78
+ validate(handlers, path, value.$value, fieldSchema)
65
79
  }
66
80
  if (value.$default !== undefined) {
67
81
  if (value.$value !== undefined) {
68
- error(path, ParseError.valueAndDefault)
82
+ error(handlers, ParseError.valueAndDefault, path)
83
+ return
69
84
  }
70
- validate(path, value.$default, fieldSchema)
85
+ validate(handlers, path, value.$default, fieldSchema)
71
86
  }
72
87
  } else {
73
- validate(path, value, fieldSchema)
88
+ validate(handlers, path, value, fieldSchema)
74
89
  }
75
90
  return value
76
91
  }
@@ -85,17 +100,19 @@ export const timestamp: Parser<'timestamp'> = async (
85
100
  noCollect
86
101
  ) => {
87
102
  if (typeof value === 'string') {
103
+ // TODO: now + 10 and stuff
88
104
  if (value === 'now') {
89
105
  value = Date.now()
90
106
  } else {
91
107
  const d = new Date(value)
92
108
  value = d.valueOf()
93
109
  if (isNaN(value)) {
94
- error(path, ParseError.incorrectFormat)
110
+ error(handlers, ParseError.incorrectFormat, path)
111
+ return
95
112
  }
96
113
  }
97
114
  }
98
- const parsedValue = shared(path, value, fieldSchema)
115
+ const parsedValue = shared(handlers, path, value, fieldSchema)
99
116
  if (!noCollect) {
100
117
  handlers.collect({
101
118
  path,
@@ -116,7 +133,7 @@ export const number: Parser<'number'> = async (
116
133
  handlers,
117
134
  noCollect
118
135
  ) => {
119
- const parsedValue = shared(path, value, fieldSchema)
136
+ const parsedValue = shared(handlers, path, value, fieldSchema)
120
137
  if (!noCollect) {
121
138
  handlers.collect({
122
139
  path,
@@ -137,7 +154,7 @@ export const integer: Parser<'integer'> = async (
137
154
  handlers,
138
155
  noCollect
139
156
  ) => {
140
- const parsedValue = shared(path, value, fieldSchema)
157
+ const parsedValue = shared(handlers, path, value, fieldSchema)
141
158
  if (!noCollect) {
142
159
  handlers.collect({
143
160
  path,
@@ -18,14 +18,23 @@ export const parseValueAndDefault: Parser<keyof BasedSchemaFields> = async (
18
18
  const parse = parsers[typeDef]
19
19
  if (value.$value !== undefined) {
20
20
  // TODO: for errors handle path a bit smarter...
21
- parse(path, value.$value, fieldSchema, typeSchema, target, handlers, true)
21
+ await parse(
22
+ path,
23
+ value.$value,
24
+ fieldSchema,
25
+ typeSchema,
26
+ target,
27
+ handlers,
28
+ true
29
+ )
22
30
  handled = true
23
31
  }
24
32
  if (value.$default !== undefined) {
25
33
  if (value.$value !== undefined) {
26
- error(path, ParseError.valueAndDefault)
34
+ error(handlers, ParseError.valueAndDefault, path)
27
35
  }
28
- parse(
36
+ console.info('->', path, 'go')
37
+ await parse(
29
38
  path,
30
39
  value.$default,
31
40
  fieldSchema,
@@ -38,6 +47,7 @@ export const parseValueAndDefault: Parser<keyof BasedSchemaFields> = async (
38
47
  }
39
48
  }
40
49
  if (handled && !noCollect) {
50
+ console.log('collect?')
41
51
  handlers.collect({ path, value, typeSchema, fieldSchema, target })
42
52
  }
43
53
  return handled
@@ -16,14 +16,15 @@ export const reference: Parser<'reference'> = async (
16
16
  // $value
17
17
 
18
18
  if (typeof value !== 'string') {
19
- error(path, ParseError.incorrectFormat)
19
+ error(handlers, ParseError.incorrectFormat, path)
20
20
  }
21
21
 
22
22
  if ('allowedTypes' in fieldSchema) {
23
23
  const prefix = value.slice(0, 2)
24
24
  const targetType = target.schema.prefixToTypeMapping[prefix]
25
25
  if (!targetType) {
26
- error(path, ParseError.referenceIsIncorrectType)
26
+ error(handlers, ParseError.referenceIsIncorrectType, path)
27
+ return
27
28
  }
28
29
  let typeMatches = false
29
30
  for (const t of fieldSchema.allowedTypes) {
@@ -37,18 +38,21 @@ export const reference: Parser<'reference'> = async (
37
38
  typeMatches = true
38
39
  if (t.$filter) {
39
40
  if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
40
- error(path, ParseError.referenceIsIncorrectType)
41
+ error(handlers, ParseError.referenceIsIncorrectType, path)
42
+ return
41
43
  }
42
44
  }
43
45
  } else if (!t.type && t.$filter) {
44
46
  if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
45
- error(path, ParseError.referenceIsIncorrectType)
47
+ error(handlers, ParseError.referenceIsIncorrectType, path)
48
+ return
46
49
  }
47
50
  }
48
51
  }
49
52
  }
50
53
  if (typeMatches === false) {
51
- error(path, ParseError.referenceIsIncorrectType)
54
+ error(handlers, ParseError.referenceIsIncorrectType, path)
55
+ return
52
56
  }
53
57
  }
54
58
  if (!noCollect) {
@@ -100,6 +104,8 @@ export const references: Parser<'references'> = async (
100
104
  })
101
105
  )
102
106
  }
107
+ } else {
108
+ error(handlers, ParseError.incorrectFormat, path)
103
109
  }
104
110
  if (!noCollect) {
105
111
  handlers.collect({ path, value, typeSchema, fieldSchema, target })
package/src/set/rest.ts CHANGED
@@ -15,7 +15,7 @@ export const cardinality: Parser<'cardinality'> = async (
15
15
  ) => {
16
16
  if (value && typeof value === 'object') {
17
17
  if (value.$default !== undefined) {
18
- error(path, ParseError.defaultNotSupported)
18
+ error(handlers, ParseError.defaultNotSupported, path)
19
19
  }
20
20
  if (value.$value !== undefined) {
21
21
  value = hashObjectIgnoreKeyOrder(value.$value).toString(16)
@@ -54,7 +54,7 @@ export const boolean: Parser<'boolean'> = async (
54
54
  }
55
55
 
56
56
  if (typeof value !== 'boolean') {
57
- error(path, ParseError.incorrectFormat)
57
+ error(handlers, ParseError.incorrectFormat, path)
58
58
  }
59
59
 
60
60
  if (!noCollect) {
@@ -91,7 +91,7 @@ export const enumParser: Parser<'enum'> = async (
91
91
  return
92
92
  }
93
93
  }
94
- error(path, ParseError.incorrectFormat)
94
+ error(handlers, ParseError.incorrectFormat, path)
95
95
  }
96
96
  }
97
97
 
@@ -130,6 +130,6 @@ export const json: Parser<'json'> = async (
130
130
  })
131
131
  }
132
132
  } catch (err) {
133
- error(path, ParseError.incorrectFormat)
133
+ error(handlers, ParseError.incorrectFormat, path)
134
134
  }
135
135
  }
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,9 +168,20 @@ 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
+ }
167
185
  return
168
186
  } else if (
169
187
  await parseValueAndDefault(
@@ -189,36 +207,30 @@ export const text: Parser<'text'> = async (
189
207
  }
190
208
 
191
209
  for (const key in value) {
192
- const newPath = [...path, key]
210
+ if (!target.schema.languages.includes(<BasedSchemaLanguage>key)) {
211
+ error(handlers, ParseError.languageNotSupported, path)
212
+ }
193
213
 
194
214
  if (typeof value[key] === 'object') {
195
- if (value[key].$value) {
196
- text(
197
- [...path, key, '$value'],
198
- value[key].$value,
215
+ if (
216
+ await parseValueAndDefault(
217
+ path,
218
+ value[key],
199
219
  fieldSchema,
200
220
  typeSchema,
201
221
  target,
202
222
  handlers,
203
223
  true
204
224
  )
225
+ ) {
226
+ continue
205
227
  }
206
- if (!noCollect) {
207
- handlers.collect({
208
- path: newPath,
209
- value: null,
210
- typeSchema,
211
- fieldSchema,
212
- target,
213
- })
214
- }
215
- continue
216
228
  }
217
229
 
218
230
  if (
219
231
  !(await parseValueAndDefault(
220
232
  path,
221
- { $language: key, [key]: value[key] },
233
+ { [key]: value[key] },
222
234
  fieldSchema,
223
235
  typeSchema,
224
236
  target,
@@ -226,17 +238,17 @@ export const text: Parser<'text'> = async (
226
238
  true
227
239
  ))
228
240
  ) {
229
- validate(newPath, value[key], fieldSchema)
241
+ validate(handlers, path, value[key], fieldSchema)
230
242
  }
243
+ }
231
244
 
232
- if (!noCollect) {
233
- handlers.collect({
234
- path: newPath,
235
- value: value[key],
236
- typeSchema,
237
- fieldSchema,
238
- target,
239
- })
240
- }
245
+ if (!noCollect) {
246
+ handlers.collect({
247
+ path,
248
+ value,
249
+ typeSchema,
250
+ fieldSchema,
251
+ target,
252
+ })
241
253
  }
242
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'
@@ -352,9 +356,9 @@ export type BasedSchemaCollectProps = {
352
356
  }
353
357
 
354
358
  export type BasedSetHandlers = {
355
- collect: (props: BasedSchemaCollectProps) => void
359
+ collectErrors: (props: { message: string; code: ParseError }) => void
356
360
 
357
- // add collectNeedRequired
361
+ collect: (props: BasedSchemaCollectProps) => void
358
362
 
359
363
  checkRequiredFields: (path: (string | number)[]) => Promise<boolean>
360
364
 
@@ -363,3 +367,8 @@ export type BasedSetHandlers = {
363
367
  $filter: any
364
368
  ) => Promise<boolean>
365
369
  }
370
+
371
+ export type BasedSetOptionalHandlers = SetOptional<
372
+ BasedSetHandlers,
373
+ 'collectErrors' | 'collect'
374
+ >