@based/schema 3.2.0 → 4.1.1

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 (89) hide show
  1. package/dist/index.d.ts +3 -5
  2. package/dist/index.js +2 -21
  3. package/dist/parse/assert.d.ts +7 -0
  4. package/dist/parse/assert.js +33 -0
  5. package/dist/parse/errors.d.ts +19 -0
  6. package/dist/parse/errors.js +19 -0
  7. package/dist/parse/index.d.ts +20 -0
  8. package/dist/parse/index.js +132 -0
  9. package/dist/parse/props.d.ts +7 -0
  10. package/dist/parse/props.js +298 -0
  11. package/dist/parse/utils.d.ts +3 -0
  12. package/dist/parse/utils.js +29 -0
  13. package/dist/parsePayload/index.d.ts +3 -0
  14. package/dist/parsePayload/index.js +2 -0
  15. package/dist/parseSchema/assert.d.ts +6 -0
  16. package/dist/parseSchema/assert.js +27 -0
  17. package/dist/parseSchema/errors.d.ts +19 -0
  18. package/dist/parseSchema/errors.js +19 -0
  19. package/dist/parseSchema/index.d.ts +20 -0
  20. package/dist/parseSchema/index.js +132 -0
  21. package/dist/parseSchema/props.d.ts +7 -0
  22. package/dist/parseSchema/props.js +256 -0
  23. package/dist/parseSchema/utils.d.ts +3 -0
  24. package/dist/parseSchema/utils.js +29 -0
  25. package/dist/src/compat/index.js +2 -2
  26. package/dist/src/set/fields/references.js +3 -30
  27. package/dist/src/types.d.ts +5 -13
  28. package/dist/src/types.js +14 -0
  29. package/dist/src/validateSchema/fieldValidators.js +4 -4
  30. package/dist/test/compat.js +1 -1
  31. package/dist/test/data/newSchemas.js +19 -21
  32. package/dist/test/reference.js +4 -2
  33. package/dist/test/walker.js +2 -2
  34. package/dist/types.d.ts +129 -194
  35. package/dist/types.js +3 -25
  36. package/package.json +14 -25
  37. package/README.md +0 -2
  38. package/dist/display/index.d.ts +0 -2
  39. package/dist/display/index.js +0 -26
  40. package/dist/display/number.d.ts +0 -3
  41. package/dist/display/number.js +0 -89
  42. package/dist/display/string.d.ts +0 -3
  43. package/dist/display/string.js +0 -23
  44. package/dist/display/timestamp.d.ts +0 -3
  45. package/dist/display/timestamp.js +0 -127
  46. package/dist/error.d.ts +0 -19
  47. package/dist/error.js +0 -24
  48. package/dist/languages.d.ts +0 -187
  49. package/dist/languages.js +0 -190
  50. package/dist/set/fields/array.d.ts +0 -2
  51. package/dist/set/fields/array.js +0 -123
  52. package/dist/set/fields/index.d.ts +0 -3
  53. package/dist/set/fields/index.js +0 -74
  54. package/dist/set/fields/number.d.ts +0 -4
  55. package/dist/set/fields/number.js +0 -129
  56. package/dist/set/fields/object.d.ts +0 -3
  57. package/dist/set/fields/object.js +0 -33
  58. package/dist/set/fields/references.d.ts +0 -3
  59. package/dist/set/fields/references.js +0 -128
  60. package/dist/set/fields/set.d.ts +0 -2
  61. package/dist/set/fields/set.js +0 -63
  62. package/dist/set/fields/string.d.ts +0 -3
  63. package/dist/set/fields/string.js +0 -284
  64. package/dist/set/index.d.ts +0 -3
  65. package/dist/set/index.js +0 -183
  66. package/dist/set/isValidId.d.ts +0 -2
  67. package/dist/set/isValidId.js +0 -21
  68. package/dist/set/types.d.ts +0 -0
  69. package/dist/set/types.js +0 -1
  70. package/dist/src/compat/newToOld.d.ts +0 -3
  71. package/dist/src/compat/newToOld.js +0 -218
  72. package/dist/src/compat/oldToNew.d.ts +0 -3
  73. package/dist/src/compat/oldToNew.js +0 -210
  74. package/dist/src/generateQuery.d.ts +0 -12
  75. package/dist/src/generateQuery.js +0 -75
  76. package/dist/test/query.d.ts +0 -1
  77. package/dist/test/query.js +0 -93
  78. package/dist/updateSchema.d.ts +0 -2
  79. package/dist/updateSchema.js +0 -16
  80. package/dist/validateSchema.d.ts +0 -4
  81. package/dist/validateSchema.js +0 -41
  82. package/dist/walker/args.d.ts +0 -36
  83. package/dist/walker/args.js +0 -162
  84. package/dist/walker/index.d.ts +0 -6
  85. package/dist/walker/index.js +0 -49
  86. package/dist/walker/parse.d.ts +0 -3
  87. package/dist/walker/parse.js +0 -186
  88. package/dist/walker/types.d.ts +0 -45
  89. package/dist/walker/types.js +0 -10
@@ -1,284 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.text = exports.string = void 0;
7
- const error_1 = require("../../error");
8
- const validator_1 = __importDefault(require("validator"));
9
- const formatPatterns = {
10
- email: validator_1.default.isEmail,
11
- URL: validator_1.default.isURL,
12
- MACAddress: validator_1.default.isMACAddress,
13
- IP: validator_1.default.isIP,
14
- IPRange: validator_1.default.isIPRange,
15
- FQDN: validator_1.default.isFQDN,
16
- IBAN: validator_1.default.isIBAN,
17
- BIC: validator_1.default.isBIC,
18
- alpha: validator_1.default.isAlpha,
19
- alphaLocales: validator_1.default.isAlphaLocales,
20
- alphanumeric: validator_1.default.isAlphanumeric,
21
- alphanumericLocales: validator_1.default.isAlphanumericLocales,
22
- passportNumber: validator_1.default.isPassportNumber,
23
- port: validator_1.default.isPort,
24
- lowercase: validator_1.default.isLowercase,
25
- uppercase: validator_1.default.isUppercase,
26
- ascii: validator_1.default.isAscii,
27
- semVer: validator_1.default.isSemVer,
28
- surrogatePair: validator_1.default.isSurrogatePair,
29
- IMEI: validator_1.default.isIMEI,
30
- hexadecimal: validator_1.default.isHexadecimal,
31
- octal: validator_1.default.isOctal,
32
- hexColor: validator_1.default.isHexColor,
33
- rgbColor: validator_1.default.isRgbColor,
34
- HSL: validator_1.default.isHSL,
35
- ISRC: validator_1.default.isISRC,
36
- MD5: validator_1.default.isMD5,
37
- JWT: validator_1.default.isJWT,
38
- UUID: validator_1.default.isUUID,
39
- luhnNumber: validator_1.default.isLuhnNumber,
40
- creditCard: validator_1.default.isCreditCard,
41
- identityCard: validator_1.default.isIdentityCard,
42
- EAN: validator_1.default.isEAN,
43
- ISIN: validator_1.default.isISIN,
44
- ISBN: validator_1.default.isISBN,
45
- ISSN: validator_1.default.isISSN,
46
- mobilePhone: validator_1.default.isMobilePhone,
47
- mobilePhoneLocales: validator_1.default.isMobilePhoneLocales,
48
- postalCode: validator_1.default.isPostalCode,
49
- postalCodeLocales: validator_1.default.isPostalCodeLocales,
50
- ethereumAddress: validator_1.default.isEthereumAddress,
51
- currency: validator_1.default.isCurrency,
52
- btcAddress: validator_1.default.isBtcAddress,
53
- ISO6391: validator_1.default.isISO6391,
54
- ISO8601: validator_1.default.isISO8601,
55
- RFC3339: validator_1.default.isRFC3339,
56
- ISO31661Alpha2: validator_1.default.isISO31661Alpha2,
57
- ISO31661Alpha3: validator_1.default.isISO31661Alpha3,
58
- ISO4217: validator_1.default.isISO4217,
59
- base32: validator_1.default.isBase32,
60
- base58: validator_1.default.isBase58,
61
- base64: validator_1.default.isBase64,
62
- dataURI: validator_1.default.isDataURI,
63
- magnetURI: validator_1.default.isMagnetURI,
64
- mimeType: validator_1.default.isMimeType,
65
- latLong: validator_1.default.isLatLong,
66
- slug: validator_1.default.isSlug,
67
- strongPassword: validator_1.default.isStrongPassword,
68
- taxID: validator_1.default.isTaxID,
69
- licensePlate: validator_1.default.isLicensePlate,
70
- VAT: validator_1.default.isVAT,
71
- };
72
- const validateString = (args, value) => {
73
- if (typeof value !== 'string') {
74
- args.error(error_1.ParseError.incorrectFormat);
75
- return false;
76
- }
77
- if (args.fieldSchema.minLength && value.length < args.fieldSchema.minLength) {
78
- args.error(error_1.ParseError.subceedsMinimum);
79
- return false;
80
- }
81
- if (args.fieldSchema.maxLength && value.length > args.fieldSchema.maxLength) {
82
- args.error(error_1.ParseError.exceedsMaximum);
83
- return false;
84
- }
85
- if (args.fieldSchema.pattern) {
86
- const re = new RegExp(args.fieldSchema.pattern);
87
- if (!re.test(value)) {
88
- args.error(error_1.ParseError.incorrectFormat);
89
- return false;
90
- }
91
- }
92
- if (args.fieldSchema.format &&
93
- !formatPatterns[args.fieldSchema.format](value)) {
94
- args.error(error_1.ParseError.incorrectFormat);
95
- return false;
96
- }
97
- return true;
98
- };
99
- const string = async (args) => {
100
- if (!validateString(args, args.value)) {
101
- return;
102
- }
103
- args.collect();
104
- };
105
- exports.string = string;
106
- // --- bla
107
- // if typeof === string
108
- const text = async (args) => {
109
- const value = args.value;
110
- args.stop();
111
- if (value === null) {
112
- args.error(error_1.ParseError.incorrectFormat);
113
- return;
114
- }
115
- if (typeof value === 'object') {
116
- for (const key in value) {
117
- if (key === '$merge') {
118
- if (typeof value.$merge !== 'boolean') {
119
- args.error(error_1.ParseError.incorrectFormat);
120
- return;
121
- }
122
- }
123
- else if (key === '$delete') {
124
- if (value[key] !== true) {
125
- args.error(error_1.ParseError.incorrectFormat);
126
- return;
127
- }
128
- args.collect({ $delete: true });
129
- return;
130
- }
131
- else if (key === '$value') {
132
- const valueArgs = args.create({
133
- path: args.path,
134
- value: args.value[key],
135
- });
136
- valueArgs._stopObject = true;
137
- await valueArgs.parse();
138
- }
139
- else if (key === '$default') {
140
- if (value[key] === null) {
141
- args.error(error_1.ParseError.incorrectFormat);
142
- return;
143
- }
144
- if (typeof value[key] === 'object') {
145
- for (const k in value[key]) {
146
- if (!validateString(args, args.value[key][k])) {
147
- args.error(error_1.ParseError.incorrectFormat);
148
- return;
149
- }
150
- args
151
- .create({
152
- key: k,
153
- fieldSchema: { type: 'string' },
154
- value: { $default: args.value[key][k] },
155
- })
156
- .collect();
157
- }
158
- }
159
- else if (typeof value[key] !== 'string') {
160
- args.error(error_1.ParseError.incorrectFormat);
161
- return;
162
- }
163
- else if (!args.target.$language) {
164
- args.error(error_1.ParseError.noLanguageFound);
165
- return;
166
- }
167
- else if (!validateString(args, value[key])) {
168
- args.error(error_1.ParseError.incorrectFormat);
169
- return;
170
- }
171
- else {
172
- args
173
- .create({
174
- fieldSchema: { type: 'string' },
175
- key: args.target.$language,
176
- value: { $default: args.value[key] },
177
- })
178
- .collect();
179
- }
180
- }
181
- else if ((args.schema.translations || [])
182
- .concat(args.schema.language)
183
- .includes(key)) {
184
- if (value[key] && typeof value[key] === 'object') {
185
- for (const k in value[key]) {
186
- if (k === '$delete') {
187
- if (value[key].$delete !== true) {
188
- args.error(error_1.ParseError.incorrectFormat);
189
- return;
190
- }
191
- args
192
- .create({
193
- key,
194
- fieldSchema: { type: 'string' },
195
- value: args.value[key],
196
- })
197
- .collect();
198
- }
199
- else if (k === '$value') {
200
- if (!validateString(args, value[key].$value)) {
201
- args.create({ key }).error(error_1.ParseError.incorrectFormat);
202
- }
203
- else {
204
- args
205
- .create({
206
- key,
207
- fieldSchema: { type: 'string' },
208
- value: args.value[key].$value,
209
- })
210
- .collect();
211
- }
212
- }
213
- else if (k === '$default') {
214
- if (!validateString(args, value[key].$default)) {
215
- args.create({ key }).error(error_1.ParseError.incorrectFormat);
216
- }
217
- else {
218
- args
219
- .create({
220
- key,
221
- fieldSchema: { type: 'string' },
222
- value: { $default: args.value[key].$default },
223
- })
224
- .collect();
225
- }
226
- }
227
- else {
228
- args
229
- .create({ path: [...args.path, key, k] })
230
- .error(error_1.ParseError.fieldDoesNotExist);
231
- return;
232
- }
233
- }
234
- }
235
- else {
236
- if (!validateString(args, args.value[key])) {
237
- args.error(error_1.ParseError.incorrectFormat);
238
- return;
239
- }
240
- args
241
- .create({
242
- key,
243
- fieldSchema: { type: 'string' },
244
- value: args.value[key],
245
- })
246
- .collect();
247
- }
248
- }
249
- else {
250
- args.create({ key }).error(error_1.ParseError.languageNotSupported);
251
- }
252
- }
253
- if (!args._stopObject) {
254
- args.collect();
255
- }
256
- return;
257
- }
258
- if (typeof value !== 'string') {
259
- args.error(error_1.ParseError.incorrectFormat);
260
- return;
261
- }
262
- if (!args.target.$language) {
263
- args.error(error_1.ParseError.noLanguageFound);
264
- return;
265
- }
266
- if (!validateString(args, args.value)) {
267
- args.error(error_1.ParseError.incorrectFormat);
268
- return;
269
- }
270
- args
271
- .create({
272
- value,
273
- key: args.target.$language,
274
- fieldSchema: { type: 'string' },
275
- })
276
- .collect();
277
- if (!args._stopObject) {
278
- args.collect({
279
- [args.target.$language]: value,
280
- });
281
- }
282
- };
283
- exports.text = text;
284
- //# sourceMappingURL=string.js.map
@@ -1,3 +0,0 @@
1
- import { BasedSchema, BasedSetTarget } from '../types';
2
- import { AsyncOperation } from '../walker';
3
- export declare const setWalker: (schema: BasedSchema, value: any, asyncOperationHandler?: AsyncOperation<BasedSetTarget>) => Promise<BasedSetTarget>;
package/dist/set/index.js DELETED
@@ -1,183 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setWalker = void 0;
4
- const error_1 = require("../error");
5
- const walker_1 = require("../walker");
6
- const fields_1 = require("./fields");
7
- const isValidId_1 = require("./isValidId");
8
- const opts = {
9
- parsers: {
10
- keys: {
11
- $delete: async (args) => {
12
- if (args.prev === args.root) {
13
- args.error(error_1.ParseError.cannotDeleteNodeFromModify);
14
- return;
15
- }
16
- if (args.value === true) {
17
- args.stop();
18
- args.prev.collect();
19
- args.prev.stop();
20
- return;
21
- }
22
- },
23
- $alias: async (args) => {
24
- if (Array.isArray(args.value)) {
25
- for (const field of args.value) {
26
- if (typeof field !== 'string') {
27
- args.error(error_1.ParseError.incorrectFormat);
28
- return;
29
- }
30
- }
31
- return;
32
- }
33
- if (typeof args.value !== 'string') {
34
- args.error(error_1.ParseError.incorrectFormat);
35
- }
36
- },
37
- $merge: async (args) => {
38
- if (typeof args.value !== 'boolean') {
39
- args.error(error_1.ParseError.incorrectFormat);
40
- return;
41
- }
42
- if (args.prev !== args.root) {
43
- args.prev.collect({ $delete: true });
44
- }
45
- return;
46
- },
47
- $id: async (args) => {
48
- if (!(0, isValidId_1.isValidId)(args.schema, args.value)) {
49
- args.error(error_1.ParseError.incorrectFormat);
50
- return;
51
- }
52
- },
53
- $language: async (args) => {
54
- if (!(args.schema.translations || [])
55
- .concat(args.schema.language)
56
- .includes(args.value)) {
57
- args.error(error_1.ParseError.languageNotSupported);
58
- return;
59
- }
60
- },
61
- $value: async (args) => {
62
- const type = args.fieldSchema?.type;
63
- if (type === 'text' || type === 'set' || type == 'references') {
64
- return;
65
- }
66
- args.prev.stop();
67
- args.stop();
68
- if (args.prev.value.$default) {
69
- args.error(error_1.ParseError.valueAndDefault);
70
- return;
71
- }
72
- return {
73
- path: args.path.slice(0, -1),
74
- value: args.value,
75
- };
76
- },
77
- $default: async (args) => {
78
- const type = args.fieldSchema?.type;
79
- if (type === 'number' || type === 'integer' || type === 'text') {
80
- // default can exist with $incr and $decr
81
- return;
82
- }
83
- args.prev.stop();
84
- args.stop();
85
- if (type === 'references' || type === 'set' || type === 'array') {
86
- const newArgs = args.create({
87
- path: args.path.slice(0, -1),
88
- skipCollection: true,
89
- });
90
- await newArgs.parse();
91
- newArgs.skipCollection = false;
92
- newArgs.value = { $default: newArgs.value };
93
- newArgs.collect();
94
- }
95
- else {
96
- const collect = args._collectOverride ?? args.root._opts.collect;
97
- const newArgs = args.create({
98
- path: args.path.slice(0, -1),
99
- collect: (a) => {
100
- if (a.path.length === args.path.length - 1) {
101
- collect(a.create({ value: { $default: a.value } }));
102
- }
103
- else {
104
- // console.info('hello', a.path) can handle this later
105
- }
106
- },
107
- });
108
- await newArgs.parse();
109
- }
110
- for (const key in args.prev.value) {
111
- if (key !== '$default') {
112
- args.prev.create({ key }).error(error_1.ParseError.fieldDoesNotExist);
113
- }
114
- }
115
- },
116
- },
117
- fields: fields_1.fields,
118
- catch: async (args) => {
119
- args.error(error_1.ParseError.fieldDoesNotExist);
120
- },
121
- },
122
- init: async (value, schema, error) => {
123
- let type;
124
- const target = {
125
- type,
126
- schema,
127
- required: [],
128
- collected: [],
129
- errors: [],
130
- };
131
- if (value.$id) {
132
- if (value.$id === 'root') {
133
- type = 'root';
134
- }
135
- else {
136
- type = schema.prefixToTypeMapping[value.$id.slice(0, 2)];
137
- }
138
- if (!type) {
139
- error(error_1.ParseError.incorrectFieldType, { target });
140
- return { target };
141
- }
142
- }
143
- else if (value.$alias) {
144
- target.$alias = value.$alias;
145
- }
146
- if (value.type) {
147
- if (type && value.type !== type) {
148
- error(error_1.ParseError.incorrectNodeType, { target });
149
- return { target };
150
- }
151
- type = value.type;
152
- }
153
- const typeSchema = type === 'root' ? schema.root : schema.types[type];
154
- if (!typeSchema) {
155
- error(error_1.ParseError.incorrectNodeType, { target });
156
- return { target };
157
- }
158
- target.type = type;
159
- target.$language = value.$language;
160
- target.$id = value.$id;
161
- if ('$merge' in value) {
162
- if (typeof value.$merge !== 'boolean') {
163
- error(error_1.ParseError.incorrectFormat, { target });
164
- }
165
- target.$merge = value.$merge;
166
- }
167
- return { target, typeSchema };
168
- },
169
- error: (code, args) => {
170
- args.target.errors.push({
171
- code,
172
- path: args.path ?? [],
173
- });
174
- },
175
- collect: (args) => {
176
- args.root.target.collected.push(args);
177
- },
178
- };
179
- const setWalker = (schema, value, asyncOperationHandler) => {
180
- return (0, walker_1.walk)(schema, opts, value, asyncOperationHandler);
181
- };
182
- exports.setWalker = setWalker;
183
- //# sourceMappingURL=index.js.map
@@ -1,2 +0,0 @@
1
- import { BasedSchema } from '../types';
2
- export declare const isValidId: (schema: BasedSchema, id: any) => boolean;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isValidId = void 0;
4
- const isValidId = (schema, id) => {
5
- if (typeof id !== 'string') {
6
- return false;
7
- }
8
- if (id === 'root') {
9
- return true;
10
- }
11
- if (id.length > 16) {
12
- return false;
13
- }
14
- const prefix = id.slice(0, 2);
15
- if (!schema.prefixToTypeMapping[prefix]) {
16
- return false;
17
- }
18
- return true;
19
- };
20
- exports.isValidId = isValidId;
21
- //# sourceMappingURL=isValidId.js.map
File without changes
package/dist/set/types.js DELETED
@@ -1 +0,0 @@
1
- //# sourceMappingURL=types.js.map
@@ -1,3 +0,0 @@
1
- import { BasedOldSchema } from './oldSchemaType.js';
2
- import { BasedSchemaPartial } from '../types.js';
3
- export declare const convertNewToOld: (schema: BasedSchemaPartial) => Partial<BasedOldSchema>;