@based/schema 2.7.1 → 3.0.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 (62) hide show
  1. package/dist/display/index.d.ts +2 -0
  2. package/dist/display/index.js +26 -0
  3. package/dist/display/number.d.ts +3 -0
  4. package/dist/display/number.js +89 -0
  5. package/dist/display/string.d.ts +3 -0
  6. package/dist/display/string.js +23 -0
  7. package/dist/display/timestamp.d.ts +3 -0
  8. package/dist/display/timestamp.js +127 -0
  9. package/dist/error.d.ts +19 -0
  10. package/dist/error.js +24 -0
  11. package/dist/index.d.ts +5 -0
  12. package/dist/index.js +22 -0
  13. package/dist/languages.d.ts +187 -0
  14. package/dist/languages.js +190 -0
  15. package/dist/set/fields/array.d.ts +2 -0
  16. package/dist/set/fields/array.js +123 -0
  17. package/dist/set/fields/index.d.ts +3 -0
  18. package/dist/set/fields/index.js +74 -0
  19. package/dist/set/fields/number.d.ts +4 -0
  20. package/dist/set/fields/number.js +129 -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/references.d.ts +3 -0
  24. package/dist/set/fields/references.js +128 -0
  25. package/dist/set/fields/set.d.ts +2 -0
  26. package/dist/set/fields/set.js +63 -0
  27. package/dist/set/fields/string.d.ts +3 -0
  28. package/dist/set/fields/string.js +284 -0
  29. package/dist/set/index.d.ts +3 -0
  30. package/dist/set/index.js +183 -0
  31. package/dist/set/isValidId.d.ts +2 -0
  32. package/dist/set/isValidId.js +21 -0
  33. package/dist/set/types.d.ts +0 -0
  34. package/dist/set/types.js +1 -0
  35. package/dist/src/compat/index.d.ts +6 -2
  36. package/dist/src/compat/index.js +208 -2
  37. package/dist/src/compat/newToOld.js +6 -7
  38. package/dist/src/compat/oldSchemaType.d.ts +28 -4
  39. package/dist/src/set/fields/array.js +4 -4
  40. package/dist/src/types.d.ts +1 -1
  41. package/dist/src/validateSchema/fieldValidators.js +1 -1
  42. package/dist/test/array.js +4 -4
  43. package/dist/test/compat.js +50 -5
  44. package/dist/test/data/newSchemas.js +5 -9
  45. package/dist/test/rest.js +1 -1
  46. package/dist/test/validateSchema/fields.js +5 -5
  47. package/dist/test/walker.js +2 -2
  48. package/dist/types.d.ts +205 -0
  49. package/dist/types.js +27 -0
  50. package/dist/updateSchema.d.ts +2 -0
  51. package/dist/updateSchema.js +16 -0
  52. package/dist/validateSchema.d.ts +4 -0
  53. package/dist/validateSchema.js +41 -0
  54. package/dist/walker/args.d.ts +36 -0
  55. package/dist/walker/args.js +162 -0
  56. package/dist/walker/index.d.ts +6 -0
  57. package/dist/walker/index.js +49 -0
  58. package/dist/walker/parse.d.ts +3 -0
  59. package/dist/walker/parse.js +186 -0
  60. package/dist/walker/types.d.ts +45 -0
  61. package/dist/walker/types.js +10 -0
  62. package/package.json +1 -1
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.languages = void 0;
4
+ exports.languages = {
5
+ ab: 'Abkhazian',
6
+ aa: 'Afar',
7
+ af: 'Afrikaans',
8
+ ak: 'Akan',
9
+ sq: 'Albanian',
10
+ am: 'Amharic',
11
+ ar: 'Arabic',
12
+ an: 'Aragonese',
13
+ hy: 'Armenian',
14
+ as: 'Assamese',
15
+ av: 'Avaric',
16
+ ae: 'Avestan',
17
+ ay: 'Aymara',
18
+ az: 'Azerbaijani',
19
+ bm: 'Bambara',
20
+ ba: 'Bashkir',
21
+ eu: 'Basque',
22
+ be: 'Belarusian',
23
+ bn: 'Bengali',
24
+ bh: 'Bihari languages',
25
+ bi: 'Bislama',
26
+ bs: 'Bosnian',
27
+ br: 'Breton',
28
+ bg: 'Bulgarian',
29
+ my: 'Burmese',
30
+ ca: 'Catalan, Valencian',
31
+ km: 'Central Khmer',
32
+ ch: 'Chamorro',
33
+ ce: 'Chechen',
34
+ ny: 'Chichewa, Chewa, Nyanja',
35
+ zh: 'Chinese',
36
+ cu: 'Church Slavonic, Old Bulgarian, Old Church Slavonic',
37
+ cv: 'Chuvash',
38
+ kw: 'Cornish',
39
+ co: 'Corsican',
40
+ cr: 'Cree',
41
+ hr: 'Croatian',
42
+ cs: 'Czech',
43
+ da: 'Danish',
44
+ dv: 'Divehi, Dhivehi, Maldivian',
45
+ nl: 'Dutch, Flemish',
46
+ dz: 'Dzongkha',
47
+ en: 'English',
48
+ eo: 'Esperanto',
49
+ et: 'Estonian',
50
+ ee: 'Ewe',
51
+ fo: 'Faroese',
52
+ fj: 'Fijian',
53
+ fi: 'Finnish',
54
+ fr: 'French',
55
+ ff: 'Fulah',
56
+ gd: 'Gaelic, Scottish Gaelic',
57
+ gl: 'Galician',
58
+ lg: 'Ganda',
59
+ ka: 'Georgian',
60
+ de: 'German',
61
+ ki: 'Gikuyu, Kikuyu',
62
+ el: 'Greek (Modern)',
63
+ kl: 'Greenlandic, Kalaallisut',
64
+ gn: 'Guarani',
65
+ gu: 'Gujarati',
66
+ ht: 'Haitian, Haitian Creole',
67
+ ha: 'Hausa',
68
+ he: 'Hebrew',
69
+ hz: 'Herero',
70
+ hi: 'Hindi',
71
+ ho: 'Hiri Motu',
72
+ hu: 'Hungarian',
73
+ is: 'Icelandic',
74
+ io: 'Ido',
75
+ ig: 'Igbo',
76
+ id: 'Indonesian',
77
+ ia: 'Interlingua (International Auxiliary Language Association)',
78
+ ie: 'Interlingue',
79
+ iu: 'Inuktitut',
80
+ ik: 'Inupiaq',
81
+ ga: 'Irish',
82
+ it: 'Italian',
83
+ ja: 'Japanese',
84
+ jv: 'Javanese',
85
+ kn: 'Kannada',
86
+ kr: 'Kanuri',
87
+ ks: 'Kashmiri',
88
+ kk: 'Kazakh',
89
+ rw: 'Kinyarwanda',
90
+ kv: 'Komi',
91
+ kg: 'Kongo',
92
+ ko: 'Korean',
93
+ kj: 'Kwanyama, Kuanyama',
94
+ ku: 'Kurdish',
95
+ ky: 'Kyrgyz',
96
+ lo: 'Lao',
97
+ la: 'Latin',
98
+ lv: 'Latvian',
99
+ lb: 'Letzeburgesch, Luxembourgish',
100
+ li: 'Limburgish, Limburgan, Limburger',
101
+ ln: 'Lingala',
102
+ lt: 'Lithuanian',
103
+ lu: 'Luba-Katanga',
104
+ mk: 'Macedonian',
105
+ mg: 'Malagasy',
106
+ ms: 'Malay',
107
+ ml: 'Malayalam',
108
+ mt: 'Maltese',
109
+ gv: 'Manx',
110
+ mi: 'Maori',
111
+ mr: 'Marathi',
112
+ mh: 'Marshallese',
113
+ ro: 'Moldovan, Moldavian, Romanian',
114
+ mn: 'Mongolian',
115
+ na: 'Nauru',
116
+ nv: 'Navajo, Navaho',
117
+ nd: 'Northern Ndebele',
118
+ ng: 'Ndonga',
119
+ ne: 'Nepali',
120
+ se: 'Northern Sami',
121
+ no: 'Norwegian',
122
+ nb: 'Norwegian Bokmål',
123
+ nn: 'Norwegian Nynorsk',
124
+ ii: 'Nuosu, Sichuan Yi',
125
+ oc: 'Occitan (post 1500)',
126
+ oj: 'Ojibwa',
127
+ or: 'Oriya',
128
+ om: 'Oromo',
129
+ os: 'Ossetian, Ossetic',
130
+ pi: 'Pali',
131
+ pa: 'Panjabi, Punjabi',
132
+ ps: 'Pashto, Pushto',
133
+ fa: 'Persian',
134
+ pl: 'Polish',
135
+ pt: 'Portuguese',
136
+ qu: 'Quechua',
137
+ rm: 'Romansh',
138
+ rn: 'Rundi',
139
+ ru: 'Russian',
140
+ sm: 'Samoan',
141
+ sg: 'Sango',
142
+ sa: 'Sanskrit',
143
+ sc: 'Sardinian',
144
+ sr: 'Serbian',
145
+ sn: 'Shona',
146
+ sd: 'Sindhi',
147
+ si: 'Sinhala, Sinhalese',
148
+ sk: 'Slovak',
149
+ sl: 'Slovenian',
150
+ so: 'Somali',
151
+ st: 'Sotho, Southern',
152
+ nr: 'South Ndebele',
153
+ es: 'Spanish, Castilian',
154
+ su: 'Sundanese',
155
+ sw: 'Swahili',
156
+ ss: 'Swati',
157
+ sv: 'Swedish',
158
+ tl: 'Tagalog',
159
+ ty: 'Tahitian',
160
+ tg: 'Tajik',
161
+ ta: 'Tamil',
162
+ tt: 'Tatar',
163
+ te: 'Telugu',
164
+ th: 'Thai',
165
+ bo: 'Tibetan',
166
+ ti: 'Tigrinya',
167
+ to: 'Tonga (Tonga Islands)',
168
+ ts: 'Tsonga',
169
+ tn: 'Tswana',
170
+ tr: 'Turkish',
171
+ tk: 'Turkmen',
172
+ tw: 'Twi',
173
+ ug: 'Uighur, Uyghur',
174
+ uk: 'Ukrainian',
175
+ ur: 'Urdu',
176
+ uz: 'Uzbek',
177
+ ve: 'Venda',
178
+ vi: 'Vietnamese',
179
+ vo: 'Volap_k',
180
+ wa: 'Walloon',
181
+ cy: 'Welsh',
182
+ fy: 'Western Frisian',
183
+ wo: 'Wolof',
184
+ xh: 'Xhosa',
185
+ yi: 'Yiddish',
186
+ yo: 'Yoruba',
187
+ za: 'Zhuang, Chuang',
188
+ zu: 'Zulu',
189
+ };
190
+ //# sourceMappingURL=languages.js.map
@@ -0,0 +1,2 @@
1
+ import { FieldParser } from '../../walker';
2
+ export declare const array: FieldParser<'array'>;
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.array = void 0;
4
+ const utils_1 = require("@saulx/utils");
5
+ const error_1 = require("../../error");
6
+ const collectOperation = (fromArgs, collected, value, makeNegative) => {
7
+ fromArgs.collect(value);
8
+ if (collected.length) {
9
+ const collect = fromArgs.root._opts.collect;
10
+ for (const args of collected) {
11
+ if (makeNegative) {
12
+ args.path[fromArgs.path.length] =
13
+ args.path[fromArgs.path.length] - makeNegative;
14
+ collect(args);
15
+ }
16
+ else {
17
+ collect(args);
18
+ }
19
+ }
20
+ }
21
+ };
22
+ const parseArray = async (args, value, idx = 0) => {
23
+ const fromValue = Array.isArray(value) ? value : [value];
24
+ const q = [];
25
+ const arr = new Array(fromValue.length);
26
+ const collectNested = ['object', 'record', 'text'].includes(args.fieldSchema.values.type);
27
+ const collected = [];
28
+ for (let i = 0; i < fromValue.length; i++) {
29
+ q.push(args.parse({
30
+ key: i + idx,
31
+ value: fromValue[i],
32
+ fieldSchema: args.fieldSchema.values,
33
+ collect: (nArgs) => {
34
+ const p = nArgs.path.slice(args.path.length);
35
+ // @ts-ignore
36
+ p[0] = p[0] - idx;
37
+ (0, utils_1.setByPath)(arr, p, nArgs.value);
38
+ if (collectNested) {
39
+ collected.push(nArgs);
40
+ }
41
+ },
42
+ }));
43
+ }
44
+ await Promise.all(q);
45
+ return { arr, collected };
46
+ };
47
+ const operations = {
48
+ $insert: async (args, value) => {
49
+ if (typeof value.$insert.$idx !== 'number') {
50
+ args.error(error_1.ParseError.incorrectFormat);
51
+ return;
52
+ }
53
+ const { collected, arr } = await parseArray(args, value.$insert.$value, value.$insert.$idx);
54
+ value.$insert.$value = arr;
55
+ collectOperation(args, collected, value);
56
+ },
57
+ $push: async (args, value) => {
58
+ const { collected, arr } = await parseArray(args, value.$push.$value ?? value.$push);
59
+ value.$push = arr;
60
+ collectOperation(args, collected, value, arr.length);
61
+ },
62
+ $unshift: async (args, value) => {
63
+ const { collected, arr } = await parseArray(args, value.$unshift.$value ?? value.$unshift);
64
+ value.$unshift = arr;
65
+ collectOperation(args, collected, value);
66
+ },
67
+ $remove: async (args, value) => {
68
+ if (typeof value.$remove.$idx !== 'number') {
69
+ args.error(error_1.ParseError.incorrectFormat);
70
+ return;
71
+ }
72
+ args.collect(value);
73
+ },
74
+ $assign: async (args, value) => {
75
+ if (typeof value.$assign !== 'object' ||
76
+ typeof value.$assign.$idx !== 'number') {
77
+ args.error(error_1.ParseError.incorrectFormat);
78
+ return;
79
+ }
80
+ await args.parse({
81
+ key: value.$assign.$idx,
82
+ value: value.$assign.$value,
83
+ fieldSchema: args.fieldSchema.values,
84
+ });
85
+ },
86
+ };
87
+ const array = async (args) => {
88
+ args.stop();
89
+ if (typeof args.value !== 'object') {
90
+ args.error(error_1.ParseError.incorrectFormat);
91
+ return;
92
+ }
93
+ let value = '$value' in args.value ? args.value.$value : args.value;
94
+ if (Array.isArray(value)) {
95
+ const q = [];
96
+ args.collect({ $delete: true });
97
+ for (let i = 0; i < value.length; i++) {
98
+ q.push(args.parse({
99
+ key: i,
100
+ value: args.value[i],
101
+ fieldSchema: args.fieldSchema.values,
102
+ }));
103
+ }
104
+ await Promise.all(q);
105
+ return;
106
+ }
107
+ let hasOperation = false;
108
+ for (const key in value) {
109
+ if (operations[key]) {
110
+ if (hasOperation) {
111
+ args.error(error_1.ParseError.multipleOperationsNotAllowed);
112
+ return;
113
+ }
114
+ await operations[key](args, value);
115
+ hasOperation = true;
116
+ }
117
+ else {
118
+ args.error(error_1.ParseError.fieldDoesNotExist);
119
+ }
120
+ }
121
+ };
122
+ exports.array = array;
123
+ //# sourceMappingURL=array.js.map
@@ -0,0 +1,3 @@
1
+ import { FieldParsers } from '../../walker';
2
+ import { BasedSetTarget } from '../../types';
3
+ export declare const fields: Partial<FieldParsers<BasedSetTarget>>;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fields = void 0;
4
+ const error_1 = require("../../error");
5
+ const array_1 = require("./array");
6
+ const object_1 = require("./object");
7
+ const number_1 = require("./number");
8
+ const string_1 = require("./string");
9
+ const references_1 = require("./references");
10
+ const set_1 = require("./set");
11
+ const hash_1 = require("@saulx/hash");
12
+ const utils_1 = require("@saulx/utils");
13
+ exports.fields = {
14
+ array: array_1.array,
15
+ object: object_1.object,
16
+ record: object_1.record,
17
+ number: number_1.number,
18
+ integer: number_1.integer,
19
+ timestamp: number_1.timestamp,
20
+ string: string_1.string,
21
+ set: set_1.set,
22
+ text: string_1.text,
23
+ reference: references_1.reference,
24
+ references: references_1.references,
25
+ cardinality: async (args) => {
26
+ let hashedValue;
27
+ if (args.value && typeof args.value === 'object') {
28
+ args.stop();
29
+ if (args.value.$default !== undefined) {
30
+ args.error(error_1.ParseError.defaultNotSupported);
31
+ return;
32
+ }
33
+ if (args.value.$value !== undefined) {
34
+ hashedValue = (0, hash_1.hashObjectIgnoreKeyOrder)(args.value.$value).toString(16);
35
+ }
36
+ else {
37
+ hashedValue = (0, hash_1.hashObjectIgnoreKeyOrder)(args.value).toString(16);
38
+ }
39
+ }
40
+ else {
41
+ hashedValue = (0, hash_1.hash)(args.value).toString(16);
42
+ }
43
+ args.collect(hashedValue);
44
+ },
45
+ boolean: async (args) => {
46
+ if (typeof args.value !== 'boolean') {
47
+ args.error(error_1.ParseError.incorrectFormat);
48
+ return;
49
+ }
50
+ args.collect();
51
+ },
52
+ json: async (args) => {
53
+ args.stop();
54
+ try {
55
+ const parsedValue = JSON.stringify(args.value);
56
+ args.collect(parsedValue);
57
+ }
58
+ catch (err) {
59
+ args.error(error_1.ParseError.invalidJSON);
60
+ }
61
+ },
62
+ enum: async (args) => {
63
+ // args.stop()
64
+ const enumValues = args.fieldSchema.enum;
65
+ for (let i = 0; i < enumValues.length; i++) {
66
+ if ((0, utils_1.deepEqual)(enumValues[i], args.value)) {
67
+ args.collect(i);
68
+ return;
69
+ }
70
+ }
71
+ args.error(error_1.ParseError.incorrectFormat);
72
+ },
73
+ };
74
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,4 @@
1
+ import { FieldParser } from '../../walker';
2
+ export declare const number: FieldParser<'number'>;
3
+ export declare const integer: FieldParser<'integer'>;
4
+ export declare const timestamp: FieldParser<'timestamp'>;
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.timestamp = exports.integer = exports.number = void 0;
4
+ const error_1 = require("../../error");
5
+ const validateNumber = (args, value, ignoreMinMax) => {
6
+ const { fieldSchema } = args;
7
+ if (typeof value !== 'number') {
8
+ args.error(error_1.ParseError.incorrectFormat);
9
+ return false;
10
+ }
11
+ if (isNaN(value)) {
12
+ args.error(error_1.ParseError.incorrectFormat);
13
+ return false;
14
+ }
15
+ if (value === Infinity || value === -Infinity) {
16
+ args.error(error_1.ParseError.infinityNotSupported);
17
+ return false;
18
+ }
19
+ if (fieldSchema.type === 'integer' && value - Math.floor(value) !== 0) {
20
+ args.error(error_1.ParseError.incorrectFormat);
21
+ return false;
22
+ }
23
+ if (fieldSchema.multipleOf &&
24
+ value / fieldSchema.multipleOf -
25
+ Math.floor(value / fieldSchema.multipleOf) !==
26
+ 0) {
27
+ args.error(error_1.ParseError.incorrectFormat);
28
+ return false;
29
+ }
30
+ if (ignoreMinMax) {
31
+ // TODO: will be handled in the actual modify command
32
+ return true;
33
+ }
34
+ if (fieldSchema.maximum) {
35
+ if (fieldSchema.exclusiveMaximum) {
36
+ if (value >= fieldSchema.maximum) {
37
+ args.error(error_1.ParseError.exceedsMaximum);
38
+ return false;
39
+ }
40
+ }
41
+ else if (value > fieldSchema.maximum) {
42
+ args.error(error_1.ParseError.exceedsMaximum);
43
+ return false;
44
+ }
45
+ }
46
+ if (fieldSchema.minimum) {
47
+ if (fieldSchema.exclusiveMinimum) {
48
+ if (value <= fieldSchema.minimum) {
49
+ args.error(error_1.ParseError.subceedsMinimum);
50
+ return false;
51
+ }
52
+ }
53
+ else if (value < fieldSchema.minimum) {
54
+ args.error(error_1.ParseError.subceedsMinimum);
55
+ return false;
56
+ }
57
+ }
58
+ return true;
59
+ };
60
+ const validate = (args, value) => {
61
+ if (value === null) {
62
+ return false;
63
+ }
64
+ if (typeof value !== 'object') {
65
+ return validateNumber(args, value);
66
+ }
67
+ if ('$value' in value) {
68
+ return;
69
+ }
70
+ args.stop();
71
+ for (const key in value) {
72
+ if (key === '$default') {
73
+ if (!validateNumber(args, value.$default)) {
74
+ return false;
75
+ }
76
+ }
77
+ else if (key === '$increment') {
78
+ if (!validateNumber(args, value.$increment, true)) {
79
+ return false;
80
+ }
81
+ }
82
+ else if (key === '$decrement') {
83
+ if (!validateNumber(args, value.$decrement, true)) {
84
+ return false;
85
+ }
86
+ }
87
+ else {
88
+ args.create({ key }).error(error_1.ParseError.fieldDoesNotExist);
89
+ return false;
90
+ }
91
+ }
92
+ return true;
93
+ };
94
+ const number = async (args) => {
95
+ if (!validate(args, args.value)) {
96
+ return;
97
+ }
98
+ args.collect();
99
+ };
100
+ exports.number = number;
101
+ const integer = async (args) => {
102
+ if (!validate(args, args.value)) {
103
+ return;
104
+ }
105
+ args.collect();
106
+ };
107
+ exports.integer = integer;
108
+ const timestamp = async (args) => {
109
+ if (typeof args.value === 'string') {
110
+ if (args.value === 'now') {
111
+ // TODO: + 1s + 10s etc
112
+ args.value = Date.now();
113
+ }
114
+ else {
115
+ const d = new Date(args.value);
116
+ args.value = d.valueOf();
117
+ if (isNaN(args.value)) {
118
+ args.error(error_1.ParseError.incorrectFormat);
119
+ return;
120
+ }
121
+ }
122
+ }
123
+ if (!validateNumber(args, args.value)) {
124
+ return;
125
+ }
126
+ args.collect();
127
+ };
128
+ exports.timestamp = timestamp;
129
+ //# sourceMappingURL=number.js.map
@@ -0,0 +1,3 @@
1
+ import { FieldParser } from '../../walker';
2
+ export declare const object: FieldParser<'object'>;
3
+ export declare const record: FieldParser<'record'>;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.record = exports.object = void 0;
4
+ const error_1 = require("../../error");
5
+ const object = async (args) => {
6
+ if (typeof args.value !== 'object' || args.value === null) {
7
+ args.error(error_1.ParseError.incorrectFormat);
8
+ return;
9
+ }
10
+ const isArray = Array.isArray(args.value);
11
+ if (isArray) {
12
+ args.error(error_1.ParseError.incorrectFormat);
13
+ return;
14
+ }
15
+ args.collect();
16
+ return args;
17
+ };
18
+ exports.object = object;
19
+ const record = async (args) => {
20
+ if (typeof args.value !== 'object' || args.value === null) {
21
+ args.error(error_1.ParseError.incorrectFormat);
22
+ return;
23
+ }
24
+ const isArray = Array.isArray(args.value);
25
+ if (isArray) {
26
+ args.error(error_1.ParseError.incorrectFormat);
27
+ return;
28
+ }
29
+ args.collect();
30
+ return args;
31
+ };
32
+ exports.record = record;
33
+ //# sourceMappingURL=object.js.map
@@ -0,0 +1,3 @@
1
+ import { FieldParser } from '../../walker';
2
+ export declare const reference: FieldParser<'reference'>;
3
+ export declare const references: FieldParser<'references'>;
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.references = exports.reference = void 0;
4
+ const error_1 = require("../../error");
5
+ const isValidId_1 = require("../isValidId");
6
+ async function parseOperator(args, key) {
7
+ if (Array.isArray(args.value[key])) {
8
+ const n = args.create({
9
+ key,
10
+ skipCollection: true,
11
+ value: args.value[key],
12
+ });
13
+ await n.parse();
14
+ if (n.value?.$value) {
15
+ return n.value.$value;
16
+ }
17
+ return [];
18
+ }
19
+ const n = args.create({
20
+ value: args.value[key],
21
+ key,
22
+ skipCollection: true,
23
+ });
24
+ await (0, exports.reference)(n);
25
+ return [n.value];
26
+ }
27
+ const typeIsAllowed = (args, type) => {
28
+ if ('allowedTypes' in args.fieldSchema) {
29
+ let typeMatches = false;
30
+ for (const t of args.fieldSchema.allowedTypes) {
31
+ if (typeof t === 'string') {
32
+ if (t === type) {
33
+ return true;
34
+ }
35
+ }
36
+ else {
37
+ if (t.type && t.type === type) {
38
+ typeMatches = true;
39
+ if (t.$filter) {
40
+ // stage on requires validation in target
41
+ // TODO: ASYNC REQUIRED HOOK
42
+ // if(!(await args.target.referenceFilterCondition(value, t.$filter))){
43
+ // error(args, ParseError.referenceIsIncorrectType)
44
+ // return
45
+ // }
46
+ }
47
+ }
48
+ else if (!t.type && t.$filter) {
49
+ // if(!(await args.target.referenceFilterCondition))
50
+ // error(args, ParseError.referenceIsIncorrectType, )
51
+ // return
52
+ }
53
+ }
54
+ }
55
+ if (typeMatches === false) {
56
+ return false;
57
+ }
58
+ }
59
+ return true;
60
+ };
61
+ const reference = async (args) => {
62
+ // TODO: setting an object here , handling $alias (both async hooks)
63
+ // Block if path contains $remove (maybe not for $alias)
64
+ if (typeof args.value === 'object') {
65
+ if (args.root._opts.asyncOperationHandler) {
66
+ if (args.value.type && !typeIsAllowed(args, args.value.type)) {
67
+ args.error(error_1.ParseError.referenceIsIncorrectType);
68
+ return;
69
+ }
70
+ if (!args.target.errors.length) {
71
+ args.value = await args.root._opts.asyncOperationHandler(args, 'modifyObject');
72
+ }
73
+ }
74
+ else {
75
+ args.error(error_1.ParseError.nestedModifyObjectNotAllowed);
76
+ return;
77
+ }
78
+ }
79
+ if (!(0, isValidId_1.isValidId)(args.schema, args.value)) {
80
+ args.error(error_1.ParseError.incorrectFormat);
81
+ return;
82
+ }
83
+ const prefix = args.value.slice(0, 2);
84
+ const targetType = args.schema.prefixToTypeMapping[prefix];
85
+ if (typeIsAllowed(args, targetType)) {
86
+ args.collect();
87
+ }
88
+ else {
89
+ args.error(error_1.ParseError.referenceIsIncorrectType);
90
+ }
91
+ };
92
+ exports.reference = reference;
93
+ const references = async (args) => {
94
+ const { value } = args;
95
+ if (typeof value !== 'object' || value === null) {
96
+ args.error(error_1.ParseError.incorrectFormat);
97
+ return;
98
+ }
99
+ args.stop();
100
+ if (Array.isArray(value)) {
101
+ const parseValues = await Promise.all(value.map(async (id, key) => {
102
+ const n = args.create({
103
+ value: id,
104
+ key,
105
+ skipCollection: true,
106
+ });
107
+ await (0, exports.reference)(n);
108
+ return n.value;
109
+ }));
110
+ args.value = { $value: parseValues };
111
+ }
112
+ else {
113
+ for (const key in args.value) {
114
+ if (key === '$add') {
115
+ args.value.$add = await parseOperator(args, key);
116
+ }
117
+ else if (key === '$remove') {
118
+ args.value.$remove = await parseOperator(args, key);
119
+ }
120
+ else {
121
+ args.create({ key }).error(error_1.ParseError.fieldDoesNotExist);
122
+ }
123
+ }
124
+ }
125
+ args.collect();
126
+ };
127
+ exports.references = references;
128
+ //# sourceMappingURL=references.js.map