@based/schema 0.0.16 → 1.0.2

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 (105) hide show
  1. package/dist/error.d.ts +18 -0
  2. package/dist/error.js +31 -0
  3. package/dist/error.js.map +1 -0
  4. package/dist/index.d.ts +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/set/collections.js +14 -32
  8. package/dist/set/collections.js.map +1 -1
  9. package/dist/set/error.d.ts +1 -2
  10. package/dist/set/error.js +3 -8
  11. package/dist/set/error.js.map +1 -1
  12. package/dist/set/fields/array.d.ts +2 -0
  13. package/dist/set/fields/array.js +96 -0
  14. package/dist/set/fields/array.js.map +1 -0
  15. package/dist/set/fields/index.d.ts +3 -0
  16. package/dist/set/fields/index.js +72 -0
  17. package/dist/set/fields/index.js.map +1 -0
  18. package/dist/set/fields/number.d.ts +4 -0
  19. package/dist/set/fields/number.js +121 -0
  20. package/dist/set/fields/number.js.map +1 -0
  21. package/dist/set/fields/object.d.ts +3 -0
  22. package/dist/set/fields/object.js +33 -0
  23. package/dist/set/fields/object.js.map +1 -0
  24. package/dist/set/fields/references.d.ts +3 -0
  25. package/dist/set/fields/references.js +106 -0
  26. package/dist/set/fields/references.js.map +1 -0
  27. package/dist/set/fields/set.d.ts +2 -0
  28. package/dist/set/fields/set.js +63 -0
  29. package/dist/set/fields/set.js.map +1 -0
  30. package/dist/set/fields/string.d.ts +3 -0
  31. package/dist/set/fields/string.js +190 -0
  32. package/dist/set/fields/string.js.map +1 -0
  33. package/dist/set/index.d.ts +2 -5
  34. package/dist/set/index.js +101 -126
  35. package/dist/set/index.js.map +1 -1
  36. package/dist/set/isValidId.d.ts +2 -0
  37. package/dist/set/isValidId.js +21 -0
  38. package/dist/set/isValidId.js.map +1 -0
  39. package/dist/set/number.js +23 -36
  40. package/dist/set/number.js.map +1 -1
  41. package/dist/set/parseDefaultAndValue.js +1 -3
  42. package/dist/set/parseDefaultAndValue.js.map +1 -1
  43. package/dist/set/references.js +5 -12
  44. package/dist/set/references.js.map +1 -1
  45. package/dist/set/rest.js +4 -4
  46. package/dist/set/rest.js.map +1 -1
  47. package/dist/set/string.js +35 -35
  48. package/dist/set/string.js.map +1 -1
  49. package/dist/set/types.d.ts +0 -5
  50. package/dist/set/types.js +0 -2
  51. package/dist/types.d.ts +7 -1
  52. package/dist/types.js.map +1 -1
  53. package/dist/walker/args.d.ts +31 -0
  54. package/dist/walker/args.js +120 -0
  55. package/dist/walker/args.js.map +1 -0
  56. package/dist/walker/index.d.ts +6 -0
  57. package/dist/walker/index.js +40 -0
  58. package/dist/walker/index.js.map +1 -0
  59. package/dist/walker/parse.d.ts +2 -0
  60. package/dist/walker/parse.js +157 -0
  61. package/dist/walker/parse.js.map +1 -0
  62. package/dist/walker/types.d.ts +44 -0
  63. package/dist/walker/types.js +9 -0
  64. package/dist/walker/types.js.map +1 -0
  65. package/package.json +2 -2
  66. package/src/{set/error.ts → error.ts} +3 -1
  67. package/src/index.ts +2 -2
  68. package/src/set/fields/array.ts +111 -0
  69. package/src/set/fields/index.ts +69 -0
  70. package/src/set/fields/number.ts +134 -0
  71. package/src/set/fields/object.ts +30 -0
  72. package/src/set/fields/references.ts +114 -0
  73. package/src/set/fields/set.ts +63 -0
  74. package/src/set/fields/string.ts +199 -0
  75. package/src/set/index.ts +105 -187
  76. package/src/set/isValidId.ts +23 -0
  77. package/src/set/types.ts +0 -20
  78. package/src/types.ts +4 -2
  79. package/src/walker/args.ts +159 -0
  80. package/src/walker/index.ts +35 -0
  81. package/src/walker/parse.ts +193 -0
  82. package/src/walker/types.ts +75 -0
  83. package/test/number.ts +289 -543
  84. package/test/reference.ts +150 -198
  85. package/test/rest.ts +227 -0
  86. package/test/string.ts +139 -183
  87. package/test/utils/index.ts +23 -0
  88. package/test/walker.ts +558 -3
  89. package/dist/set2/index.d.ts +0 -0
  90. package/dist/set2/index.js +0 -71
  91. package/dist/set2/index.js.map +0 -1
  92. package/dist/walker.d.ts +0 -51
  93. package/dist/walker.js +0 -120
  94. package/dist/walker.js.map +0 -1
  95. package/src/set/collections.ts +0 -338
  96. package/src/set/number.ts +0 -167
  97. package/src/set/parseDefaultAndValue.ts +0 -54
  98. package/src/set/parsers.ts +0 -20
  99. package/src/set/references.ts +0 -113
  100. package/src/set/rest.ts +0 -135
  101. package/src/set/string.ts +0 -254
  102. package/src/set2/index.ts +0 -71
  103. package/src/walker.ts +0 -201
  104. package/test/setWalker.ts +0 -494
  105. package/test/text.ts +0 -171
@@ -1,54 +0,0 @@
1
- import { BasedSchemaFields } from '../types'
2
- import { Parser } from './types'
3
- import { error, ParseError } from './error'
4
- import parsers from './parsers'
5
-
6
- export const parseValueAndDefault: Parser<keyof BasedSchemaFields> = async (
7
- path,
8
- value,
9
- fieldSchema,
10
- typeSchema,
11
- target,
12
- handlers,
13
- noCollect
14
- ): Promise<boolean> => {
15
- let handled = false
16
- if (typeof value === 'object') {
17
- const typeDef = fieldSchema.type ?? ('enum' in fieldSchema ? 'enum' : '')
18
- const parse = parsers[typeDef]
19
- if (value.$value !== undefined) {
20
- // TODO: for errors handle path a bit smarter...
21
- await parse(
22
- path,
23
- value.$value,
24
- fieldSchema,
25
- typeSchema,
26
- target,
27
- handlers,
28
- true
29
- )
30
- handled = true
31
- }
32
- if (value.$default !== undefined) {
33
- if (value.$value !== undefined) {
34
- error(handlers, ParseError.valueAndDefault, path)
35
- }
36
- console.info('->', path, 'go')
37
- await parse(
38
- path,
39
- value.$default,
40
- fieldSchema,
41
- typeSchema,
42
- target,
43
- handlers,
44
- true
45
- )
46
- handled = true
47
- }
48
- }
49
- if (handled && !noCollect) {
50
- console.log('collect?')
51
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
52
- }
53
- return handled
54
- }
@@ -1,20 +0,0 @@
1
- import { Parsers } from './types'
2
- import * as references from './references'
3
- import * as collections from './collections'
4
- import * as number from './number'
5
- import * as string from './string'
6
-
7
- import { enumParser, boolean, cardinality, json } from './rest'
8
-
9
- const parsers: Parsers = {
10
- ...string,
11
- ...references,
12
- ...collections,
13
- ...number,
14
- enum: enumParser,
15
- boolean,
16
- cardinality,
17
- json,
18
- }
19
-
20
- export default parsers
@@ -1,113 +0,0 @@
1
- import { Parser } from './types'
2
- import { error, ParseError } from './error'
3
-
4
- export const reference: Parser<'reference'> = async (
5
- path,
6
- value,
7
- fieldSchema,
8
- typeSchema,
9
- target,
10
- handlers,
11
- noCollect
12
- ) => {
13
- // $no root
14
- // prob pass these as options
15
- // value .default
16
- // $value
17
-
18
- if (typeof value !== 'string') {
19
- error(handlers, ParseError.incorrectFormat, path)
20
- }
21
-
22
- if ('allowedTypes' in fieldSchema) {
23
- const prefix = value.slice(0, 2)
24
- const targetType = target.schema.prefixToTypeMapping[prefix]
25
- if (!targetType) {
26
- error(handlers, ParseError.referenceIsIncorrectType, path)
27
- return
28
- }
29
- let typeMatches = false
30
- for (const t of fieldSchema.allowedTypes) {
31
- if (typeof t === 'string') {
32
- if (t === targetType) {
33
- typeMatches = true
34
- break
35
- }
36
- } else {
37
- if (t.type && t.type === targetType) {
38
- typeMatches = true
39
- if (t.$filter) {
40
- if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
41
- error(handlers, ParseError.referenceIsIncorrectType, path)
42
- return
43
- }
44
- }
45
- } else if (!t.type && t.$filter) {
46
- if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
47
- error(handlers, ParseError.referenceIsIncorrectType, path)
48
- return
49
- }
50
- }
51
- }
52
- }
53
- if (typeMatches === false) {
54
- error(handlers, ParseError.referenceIsIncorrectType, path)
55
- return
56
- }
57
- }
58
- if (!noCollect) {
59
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
60
- }
61
- }
62
-
63
- export const references: Parser<'references'> = async (
64
- path,
65
- value,
66
- fieldSchema,
67
- typeSchema,
68
- target,
69
- handlers,
70
- noCollect
71
- ) => {
72
- // default
73
- // $no root
74
- if (Array.isArray(value)) {
75
- await Promise.all(
76
- value.map((v, i) => {
77
- return reference(
78
- [...path, i],
79
- v,
80
- // not nice slow
81
- { ...fieldSchema, type: 'reference' },
82
- typeSchema,
83
- target,
84
- handlers,
85
- true
86
- )
87
- })
88
- )
89
- value = { $value: value }
90
- } else if (typeof value === 'object') {
91
- if (value.$add) {
92
- await Promise.all(
93
- value.$add.map((v, i) => {
94
- return reference(
95
- [...path, '$add', i],
96
- v,
97
- // not nice slow
98
- { ...fieldSchema, type: 'reference' },
99
- typeSchema,
100
- target,
101
- handlers,
102
- true
103
- )
104
- })
105
- )
106
- }
107
- } else {
108
- error(handlers, ParseError.incorrectFormat, path)
109
- }
110
- if (!noCollect) {
111
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
112
- }
113
- }
package/src/set/rest.ts DELETED
@@ -1,135 +0,0 @@
1
- import { Parser } from './types'
2
- import { deepEqual } from '@saulx/utils'
3
- import { hashObjectIgnoreKeyOrder, hash } from '@saulx/hash'
4
- import { error, ParseError } from './error'
5
- import { parseValueAndDefault } from './parseDefaultAndValue'
6
-
7
- export const cardinality: Parser<'cardinality'> = async (
8
- path,
9
- value,
10
- fieldSchema,
11
- typeSchema,
12
- target,
13
- handlers,
14
- noCollect
15
- ) => {
16
- if (value && typeof value === 'object') {
17
- if (value.$default !== undefined) {
18
- error(handlers, ParseError.defaultNotSupported, path)
19
- }
20
- if (value.$value !== undefined) {
21
- value = hashObjectIgnoreKeyOrder(value.$value).toString(16)
22
- } else {
23
- value = hashObjectIgnoreKeyOrder(value).toString(16)
24
- }
25
- } else {
26
- value = hash(value).toString(16)
27
- }
28
- if (!noCollect) {
29
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
30
- }
31
- }
32
-
33
- export const boolean: Parser<'boolean'> = async (
34
- path,
35
- value,
36
- fieldSchema,
37
- typeSchema,
38
- target,
39
- handlers,
40
- noCollect
41
- ) => {
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
-
56
- if (typeof value !== 'boolean') {
57
- error(handlers, ParseError.incorrectFormat, path)
58
- }
59
-
60
- if (!noCollect) {
61
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
62
- }
63
- }
64
-
65
- export const enumParser: Parser<'enum'> = async (
66
- path,
67
- value,
68
- fieldSchema,
69
- typeSchema,
70
- target,
71
- handlers,
72
- noCollect
73
- ) => {
74
- const enumValues = fieldSchema.enum
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
- }
93
- }
94
- error(handlers, ParseError.incorrectFormat, path)
95
- }
96
- }
97
-
98
- export const json: Parser<'json'> = async (
99
- path,
100
- value,
101
- fieldSchema,
102
- typeSchema,
103
- target,
104
- handlers,
105
- noCollect
106
- ) => {
107
- if (
108
- await parseValueAndDefault(
109
- path,
110
- value,
111
- fieldSchema,
112
- typeSchema,
113
- target,
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
- }
132
- } catch (err) {
133
- error(handlers, ParseError.incorrectFormat, path)
134
- }
135
- }
package/src/set/string.ts DELETED
@@ -1,254 +0,0 @@
1
- import { Parser } from './types'
2
- import { error, ParseError } from './error'
3
- import {
4
- BasedSchemaFieldString,
5
- BasedSchemaFieldText,
6
- BasedSchemaLanguage,
7
- BasedSetHandlers,
8
- } from '../types'
9
- import validators from 'validator'
10
- import { parseValueAndDefault } from './parseDefaultAndValue'
11
-
12
- const formatPatterns: Record<
13
- BasedSchemaFieldString['format'],
14
- (str: string) => boolean
15
- > = {
16
- email: validators.isEmail,
17
- URL: validators.isURL,
18
- MACAddress: validators.isMACAddress,
19
- IP: validators.isIP,
20
- IPRange: validators.isIPRange,
21
- FQDN: validators.isFQDN,
22
- IBAN: validators.isIBAN,
23
- BIC: validators.isBIC,
24
- alpha: validators.isAlpha,
25
- alphaLocales: validators.isAlphaLocales,
26
- alphanumeric: validators.isAlphanumeric,
27
- alphanumericLocales: validators.isAlphanumericLocales,
28
- passportNumber: validators.isPassportNumber,
29
- port: validators.isPort,
30
- lowercase: validators.isLowercase,
31
- uppercase: validators.isUppercase,
32
- ascii: validators.isAscii,
33
- semVer: validators.isSemVer,
34
- surrogatePair: validators.isSurrogatePair,
35
- IMEI: validators.isIMEI,
36
- hexadecimal: validators.isHexadecimal,
37
- octal: validators.isOctal,
38
- hexColor: validators.isHexColor,
39
- rgbColor: validators.isRgbColor,
40
- HSL: validators.isHSL,
41
- ISRC: validators.isISRC,
42
- MD5: validators.isMD5,
43
- JWT: validators.isJWT,
44
- UUID: validators.isUUID,
45
- luhnNumber: validators.isLuhnNumber,
46
- creditCard: validators.isCreditCard,
47
- identityCard: validators.isIdentityCard,
48
- EAN: validators.isEAN,
49
- ISIN: validators.isISIN,
50
- ISBN: validators.isISBN,
51
- ISSN: validators.isISSN,
52
- mobilePhone: validators.isMobilePhone,
53
- mobilePhoneLocales: validators.isMobilePhoneLocales,
54
- postalCode: validators.isPostalCode,
55
- postalCodeLocales: validators.isPostalCodeLocales,
56
- ethereumAddress: validators.isEthereumAddress,
57
- currency: validators.isCurrency,
58
- btcAddress: validators.isBtcAddress,
59
- ISO6391: validators.isISO6391,
60
- ISO8601: validators.isISO8601,
61
- RFC3339: validators.isRFC3339,
62
- ISO31661Alpha2: validators.isISO31661Alpha2,
63
- ISO31661Alpha3: validators.isISO31661Alpha3,
64
- ISO4217: validators.isISO4217,
65
- base32: validators.isBase32,
66
- base58: validators.isBase58,
67
- base64: validators.isBase64,
68
- dataURI: validators.isDataURI,
69
- magnetURI: validators.isMagnetURI,
70
- mimeType: validators.isMimeType,
71
- latLong: validators.isLatLong,
72
- slug: validators.isSlug,
73
- strongPassword: validators.isStrongPassword,
74
- taxID: validators.isTaxID,
75
- licensePlate: validators.isLicensePlate,
76
- VAT: validators.isVAT,
77
- }
78
-
79
- const validate = (
80
- handlers: BasedSetHandlers,
81
- path: (string | number)[],
82
- value: string,
83
- fieldSchema: BasedSchemaFieldText | BasedSchemaFieldString
84
- ) => {
85
- if (typeof value !== 'string') {
86
- error(handlers, ParseError.incorrectFormat, path)
87
- }
88
- if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
89
- error(handlers, ParseError.subceedsMinimum, path)
90
- }
91
- if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
92
- error(handlers, ParseError.exceedsMaximum, path)
93
- }
94
- if (fieldSchema.pattern) {
95
- const re = new RegExp(fieldSchema.pattern)
96
- if (!re.test(value)) {
97
- error(handlers, ParseError.incorrectFormat, path)
98
- }
99
- }
100
- if (fieldSchema.format && !formatPatterns[fieldSchema.format](value)) {
101
- error(handlers, ParseError.incorrectFormat, path)
102
- }
103
- // return true / false and add collectError
104
- }
105
-
106
- export const string: Parser<'string'> = async (
107
- path,
108
- value,
109
- fieldSchema,
110
- typeSchema,
111
- target,
112
- handlers,
113
- noCollect
114
- ) => {
115
- if (
116
- await parseValueAndDefault(
117
- path,
118
- value,
119
- fieldSchema,
120
- typeSchema,
121
- target,
122
- handlers,
123
- noCollect
124
- )
125
- ) {
126
- return
127
- }
128
- validate(handlers, path, value, fieldSchema)
129
- if (!noCollect) {
130
- handlers.collect({ path, value, typeSchema, fieldSchema, target })
131
- }
132
- }
133
-
134
- export const text: Parser<'text'> = async (
135
- path,
136
- value,
137
- fieldSchema,
138
- typeSchema,
139
- target,
140
- handlers,
141
- noCollect
142
- ) => {
143
- const valueType = typeof value
144
- if (target.$language && valueType === 'string') {
145
- validate(handlers, path, value, fieldSchema)
146
- if (!noCollect) {
147
- handlers.collect({
148
- path,
149
- value: { [target.$language]: value },
150
- typeSchema,
151
- fieldSchema,
152
- target,
153
- })
154
- }
155
- return
156
- }
157
-
158
- if (valueType !== 'object') {
159
- error(handlers, ParseError.incorrectFormat, path)
160
- }
161
-
162
- if (
163
- target.$language &&
164
- (await parseValueAndDefault(
165
- path,
166
- value,
167
- fieldSchema,
168
- typeSchema,
169
- target,
170
- handlers,
171
- true
172
- ))
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
- }
206
- return
207
- }
208
-
209
- for (const key in value) {
210
- if (!target.schema.languages.includes(<BasedSchemaLanguage>key)) {
211
- error(handlers, ParseError.languageNotSupported, path)
212
- }
213
-
214
- if (typeof value[key] === 'object') {
215
- if (
216
- await parseValueAndDefault(
217
- path,
218
- value[key],
219
- fieldSchema,
220
- typeSchema,
221
- target,
222
- handlers,
223
- true
224
- )
225
- ) {
226
- continue
227
- }
228
- }
229
-
230
- if (
231
- !(await parseValueAndDefault(
232
- path,
233
- { [key]: value[key] },
234
- fieldSchema,
235
- typeSchema,
236
- target,
237
- handlers,
238
- true
239
- ))
240
- ) {
241
- validate(handlers, path, value[key], fieldSchema)
242
- }
243
- }
244
-
245
- if (!noCollect) {
246
- handlers.collect({
247
- path,
248
- value,
249
- typeSchema,
250
- fieldSchema,
251
- target,
252
- })
253
- }
254
- }
package/src/set2/index.ts DELETED
@@ -1,71 +0,0 @@
1
- // import { ParseError } from '../set/error'
2
- // import { BasedSchema, BasedSetTarget } from '../types'
3
- // import { walk } from '../walker'
4
-
5
- // export const setWalker2 = (schema: BasedSchema, value: any) => {
6
- // return walk<BasedSetTarget>(
7
- // {
8
- // schema,
9
- // parsers: {
10
- // keys: {},
11
- // fields: {
12
- // object: async (args) => {
13
- // if (typeof value !== 'object') {
14
- // args.error(args, ParseError.incorrectFormat)
15
- // return
16
- // }
17
- // const isArray = Array.isArray(value)
18
- // if (isArray) {
19
- // args.error(args, ParseError.incorrectFormat)
20
- // return
21
- // }
22
- // const q: Promise<any>[] = []
23
- // for (const key in value) {
24
- // q.push(args.parse(args, key, value[key]))
25
- // }
26
- // await Promise.all(q)
27
- // },
28
- // boolean: async (args) => {
29
- // if (typeof value !== 'boolean') {
30
- // args.error(args, ParseError.incorrectFormat)
31
- // return
32
- // }
33
- // args.collect(args)
34
- // },
35
- // },
36
- // any: async (args) => {
37
- // args.error(args, ParseError.fieldDoesNotExist)
38
- // },
39
- // },
40
- // init: async (value, args) => {
41
- // let type: string
42
- // if (value.$id) {
43
- // type = schema.prefixToTypeMapping[value.$id.slice(0, 2)]
44
- // if (!type) {
45
- // args.error(args, ParseError.incorrectFieldType)
46
- // return
47
- // }
48
- // }
49
- // if (value.type) {
50
- // if (type && value.type !== type) {
51
- // args.error(args, ParseError.incorrectNodeType)
52
- // return
53
- // }
54
- // type = value.type
55
- // }
56
- // const schemaType = schema.types[type]
57
- // if (!schemaType) {
58
- // args.error(args, ParseError.incorrectNodeType)
59
- // return
60
- // }
61
- // const target: BasedSetTarget = {
62
- // type,
63
- // schema,
64
- // required: [],
65
- // }
66
- // return { ...args, target }
67
- // },
68
- // },
69
- // value
70
- // )
71
- // }