schematox 0.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.
- package/.eslintrc.json +21 -0
- package/LICENSE +21 -0
- package/README.md +373 -0
- package/dist/base-schema-parser.d.ts +6 -0
- package/dist/base-schema-parser.js +325 -0
- package/dist/base-schema-validator.d.ts +6 -0
- package/dist/base-schema-validator.js +246 -0
- package/dist/error.d.ts +54 -0
- package/dist/error.js +33 -0
- package/dist/general-schema-parser.d.ts +4 -0
- package/dist/general-schema-parser.js +125 -0
- package/dist/general-schema-validator.d.ts +4 -0
- package/dist/general-schema-validator.js +116 -0
- package/dist/index.js +36 -0
- package/dist/index.ts +56 -0
- package/dist/programmatic-schema/array.d.ts +23 -0
- package/dist/programmatic-schema/array.js +40 -0
- package/dist/programmatic-schema/boolean.d.ts +57 -0
- package/dist/programmatic-schema/boolean.js +57 -0
- package/dist/programmatic-schema/buffer.d.ts +72 -0
- package/dist/programmatic-schema/buffer.js +61 -0
- package/dist/programmatic-schema/number-union.d.ts +66 -0
- package/dist/programmatic-schema/number-union.js +61 -0
- package/dist/programmatic-schema/number.d.ts +81 -0
- package/dist/programmatic-schema/number.js +71 -0
- package/dist/programmatic-schema/object.d.ts +25 -0
- package/dist/programmatic-schema/object.js +43 -0
- package/dist/programmatic-schema/string-union.d.ts +66 -0
- package/dist/programmatic-schema/string-union.js +61 -0
- package/dist/programmatic-schema/string.d.ts +81 -0
- package/dist/programmatic-schema/string.js +71 -0
- package/dist/types/base-detailed-schema-types.ts +135 -0
- package/dist/types/base-short-schema-types.ts +53 -0
- package/dist/types/compound-schema-types.ts +156 -0
- package/dist/utils/fp.d.ts +14 -0
- package/dist/utils/fp.js +19 -0
- package/dist/x-closure.d.ts +8 -0
- package/dist/x-closure.js +18 -0
- package/package.json +48 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseBaseSchemaSubject = void 0;
|
|
4
|
+
var fp_1 = require("./utils/fp");
|
|
5
|
+
var error_1 = require("./error");
|
|
6
|
+
function parseBaseSchemaSubject(schema, subject) {
|
|
7
|
+
if (typeof schema === 'string') {
|
|
8
|
+
switch (schema) {
|
|
9
|
+
case 'string?':
|
|
10
|
+
case 'string': {
|
|
11
|
+
if (typeof subject !== 'string') {
|
|
12
|
+
if (subject === null || subject === undefined) {
|
|
13
|
+
if (schema === 'string?') {
|
|
14
|
+
return (0, fp_1.data)(undefined);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return (0, fp_1.error)({
|
|
18
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
19
|
+
schema: schema,
|
|
20
|
+
subject: subject,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return (0, fp_1.data)(subject);
|
|
24
|
+
}
|
|
25
|
+
case 'number?':
|
|
26
|
+
case 'number': {
|
|
27
|
+
if (typeof subject !== 'number') {
|
|
28
|
+
if (subject === null || subject === undefined) {
|
|
29
|
+
if (schema === 'number?') {
|
|
30
|
+
return (0, fp_1.data)(undefined);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return (0, fp_1.error)({
|
|
34
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
35
|
+
schema: schema,
|
|
36
|
+
subject: subject,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (Number.isNaN(subject)) {
|
|
40
|
+
return (0, fp_1.error)({
|
|
41
|
+
code: error_1.PARSE_ERROR_CODE.NaN,
|
|
42
|
+
schema: schema,
|
|
43
|
+
subject: subject,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (Number.isFinite(subject) === false) {
|
|
47
|
+
return (0, fp_1.error)({
|
|
48
|
+
code: error_1.PARSE_ERROR_CODE.infinity,
|
|
49
|
+
schema: schema,
|
|
50
|
+
subject: subject,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return (0, fp_1.data)(subject);
|
|
54
|
+
}
|
|
55
|
+
case 'boolean?':
|
|
56
|
+
case 'boolean': {
|
|
57
|
+
if (typeof subject !== 'boolean') {
|
|
58
|
+
if (subject === null || subject === undefined) {
|
|
59
|
+
if (schema === 'boolean?') {
|
|
60
|
+
return (0, fp_1.data)(undefined);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return (0, fp_1.error)({
|
|
64
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
65
|
+
schema: schema,
|
|
66
|
+
subject: subject,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return (0, fp_1.data)(subject);
|
|
70
|
+
}
|
|
71
|
+
case 'buffer?':
|
|
72
|
+
case 'buffer': {
|
|
73
|
+
if (Buffer.isBuffer(subject) === false) {
|
|
74
|
+
if (subject === null || subject === undefined) {
|
|
75
|
+
if (schema === 'buffer?') {
|
|
76
|
+
return (0, fp_1.data)(undefined);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return (0, fp_1.error)({
|
|
80
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
81
|
+
schema: schema,
|
|
82
|
+
subject: subject,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return (0, fp_1.data)(subject);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
switch (schema.type) {
|
|
90
|
+
case 'string': {
|
|
91
|
+
if (typeof subject !== 'string') {
|
|
92
|
+
if (subject === undefined || subject === null) {
|
|
93
|
+
if (schema.optional) {
|
|
94
|
+
if (typeof schema.default === 'string') {
|
|
95
|
+
var rangeError_1 = getStringRangeError(schema, schema.default, true);
|
|
96
|
+
if (rangeError_1) {
|
|
97
|
+
return (0, fp_1.error)(rangeError_1);
|
|
98
|
+
}
|
|
99
|
+
return (0, fp_1.data)(schema.default);
|
|
100
|
+
}
|
|
101
|
+
return (0, fp_1.data)(undefined);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return (0, fp_1.error)({
|
|
105
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
106
|
+
schema: schema,
|
|
107
|
+
subject: subject,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
var rangeError = getStringRangeError(schema, subject, false);
|
|
111
|
+
if (rangeError) {
|
|
112
|
+
return (0, fp_1.error)(rangeError);
|
|
113
|
+
}
|
|
114
|
+
return (0, fp_1.data)(subject);
|
|
115
|
+
}
|
|
116
|
+
case 'number': {
|
|
117
|
+
if (typeof subject !== 'number') {
|
|
118
|
+
if (subject === undefined || subject === null) {
|
|
119
|
+
if (schema.optional) {
|
|
120
|
+
if (typeof schema.default === 'number') {
|
|
121
|
+
var rangeError_2 = getNumberRangeError(schema, schema.default, true);
|
|
122
|
+
if (rangeError_2) {
|
|
123
|
+
return (0, fp_1.error)(rangeError_2);
|
|
124
|
+
}
|
|
125
|
+
return (0, fp_1.data)(schema.default);
|
|
126
|
+
}
|
|
127
|
+
return (0, fp_1.data)(undefined);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return (0, fp_1.error)({
|
|
131
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
132
|
+
schema: schema,
|
|
133
|
+
subject: subject,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
if (Number.isNaN(subject)) {
|
|
137
|
+
return (0, fp_1.error)({
|
|
138
|
+
code: error_1.PARSE_ERROR_CODE.NaN,
|
|
139
|
+
schema: schema,
|
|
140
|
+
subject: subject,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
if (Number.isFinite(subject) === false) {
|
|
144
|
+
return (0, fp_1.error)({
|
|
145
|
+
code: error_1.PARSE_ERROR_CODE.infinity,
|
|
146
|
+
schema: schema,
|
|
147
|
+
subject: subject,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
var rangeError = getNumberRangeError(schema, subject, false);
|
|
151
|
+
if (rangeError) {
|
|
152
|
+
return (0, fp_1.error)(rangeError);
|
|
153
|
+
}
|
|
154
|
+
return (0, fp_1.data)(subject);
|
|
155
|
+
}
|
|
156
|
+
case 'boolean': {
|
|
157
|
+
if (typeof subject !== 'boolean') {
|
|
158
|
+
if (subject === undefined || subject === null) {
|
|
159
|
+
if (schema.optional) {
|
|
160
|
+
if (typeof schema.default === 'boolean') {
|
|
161
|
+
return (0, fp_1.data)(schema.default);
|
|
162
|
+
}
|
|
163
|
+
return (0, fp_1.data)(undefined);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return (0, fp_1.error)({
|
|
167
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
168
|
+
schema: schema,
|
|
169
|
+
subject: subject,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
return (0, fp_1.data)(subject);
|
|
173
|
+
}
|
|
174
|
+
case 'buffer': {
|
|
175
|
+
if (Buffer.isBuffer(subject) === false) {
|
|
176
|
+
if (subject === undefined || subject === null) {
|
|
177
|
+
if (schema.optional) {
|
|
178
|
+
return (0, fp_1.data)(undefined);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return (0, fp_1.error)({
|
|
182
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
183
|
+
schema: schema,
|
|
184
|
+
subject: subject,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
if (typeof schema.minLength === 'number') {
|
|
188
|
+
if (subject.length < schema.minLength) {
|
|
189
|
+
return (0, fp_1.error)({
|
|
190
|
+
code: error_1.PARSE_ERROR_CODE.minRange,
|
|
191
|
+
schema: schema,
|
|
192
|
+
subject: subject,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (typeof schema.maxLength === 'number') {
|
|
197
|
+
if (subject.length > schema.maxLength) {
|
|
198
|
+
return (0, fp_1.error)({
|
|
199
|
+
code: error_1.PARSE_ERROR_CODE.maxRange,
|
|
200
|
+
schema: schema,
|
|
201
|
+
subject: subject,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return (0, fp_1.data)(subject);
|
|
206
|
+
}
|
|
207
|
+
case 'stringUnion': {
|
|
208
|
+
if (typeof subject !== 'string') {
|
|
209
|
+
if (subject === undefined || subject === null) {
|
|
210
|
+
if (schema.optional) {
|
|
211
|
+
if (typeof schema.default === 'string') {
|
|
212
|
+
var unionSet_1 = new Set(schema.of);
|
|
213
|
+
if (unionSet_1.has(schema.default) === false) {
|
|
214
|
+
return (0, fp_1.error)({
|
|
215
|
+
code: error_1.PARSE_ERROR_CODE.schemaDefaultNotInUnion,
|
|
216
|
+
subject: schema.default,
|
|
217
|
+
schema: schema,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
return (0, fp_1.data)(schema.default);
|
|
221
|
+
}
|
|
222
|
+
return (0, fp_1.data)(undefined);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return (0, fp_1.error)({
|
|
226
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
227
|
+
schema: schema,
|
|
228
|
+
subject: subject,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
var unionSet = new Set(schema.of);
|
|
232
|
+
if (unionSet.has(subject) === false) {
|
|
233
|
+
return (0, fp_1.error)({
|
|
234
|
+
code: error_1.PARSE_ERROR_CODE.notInUnion,
|
|
235
|
+
schema: schema,
|
|
236
|
+
subject: subject,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
return (0, fp_1.data)(subject);
|
|
240
|
+
}
|
|
241
|
+
case 'numberUnion': {
|
|
242
|
+
if (typeof subject !== 'number') {
|
|
243
|
+
if (subject === undefined || subject === null) {
|
|
244
|
+
if (schema.optional) {
|
|
245
|
+
if (typeof schema.default === 'number') {
|
|
246
|
+
var unionSet_2 = new Set(schema.of);
|
|
247
|
+
if (unionSet_2.has(schema.default) === false) {
|
|
248
|
+
return (0, fp_1.error)({
|
|
249
|
+
code: error_1.PARSE_ERROR_CODE.schemaDefaultNotInUnion,
|
|
250
|
+
subject: schema.default,
|
|
251
|
+
schema: schema,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
return (0, fp_1.data)(schema.default);
|
|
255
|
+
}
|
|
256
|
+
return (0, fp_1.data)(undefined);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return (0, fp_1.error)({
|
|
260
|
+
code: error_1.PARSE_ERROR_CODE.invalidType,
|
|
261
|
+
schema: schema,
|
|
262
|
+
subject: subject,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
var unionSet = new Set(schema.of);
|
|
266
|
+
if (unionSet.has(subject) === false) {
|
|
267
|
+
return (0, fp_1.error)({
|
|
268
|
+
code: error_1.PARSE_ERROR_CODE.notInUnion,
|
|
269
|
+
schema: schema,
|
|
270
|
+
subject: subject,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return (0, fp_1.data)(subject);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
exports.parseBaseSchemaSubject = parseBaseSchemaSubject;
|
|
278
|
+
function getStringRangeError(schema, subject, isSubjectFromSchemaDefault) {
|
|
279
|
+
if (typeof schema.minLength === 'number') {
|
|
280
|
+
if (subject.length < schema.minLength) {
|
|
281
|
+
return {
|
|
282
|
+
code: isSubjectFromSchemaDefault
|
|
283
|
+
? error_1.PARSE_ERROR_CODE.schemaDefaultMinRange
|
|
284
|
+
: error_1.PARSE_ERROR_CODE.minRange,
|
|
285
|
+
schema: schema,
|
|
286
|
+
subject: subject,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (typeof schema.maxLength === 'number') {
|
|
291
|
+
if (subject.length > schema.maxLength) {
|
|
292
|
+
return {
|
|
293
|
+
code: isSubjectFromSchemaDefault
|
|
294
|
+
? error_1.PARSE_ERROR_CODE.schemaDefaultMaxRange
|
|
295
|
+
: error_1.PARSE_ERROR_CODE.maxRange,
|
|
296
|
+
schema: schema,
|
|
297
|
+
subject: subject,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function getNumberRangeError(schema, subject, isSubjectFromSchemaDefault) {
|
|
303
|
+
if (typeof schema.min === 'number') {
|
|
304
|
+
if (subject < schema.min) {
|
|
305
|
+
return {
|
|
306
|
+
code: isSubjectFromSchemaDefault
|
|
307
|
+
? error_1.PARSE_ERROR_CODE.schemaDefaultMinRange
|
|
308
|
+
: error_1.PARSE_ERROR_CODE.minRange,
|
|
309
|
+
schema: schema,
|
|
310
|
+
subject: subject,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (typeof schema.max === 'number') {
|
|
315
|
+
if (subject > schema.max) {
|
|
316
|
+
return {
|
|
317
|
+
code: isSubjectFromSchemaDefault
|
|
318
|
+
? error_1.PARSE_ERROR_CODE.schemaDefaultMaxRange
|
|
319
|
+
: error_1.PARSE_ERROR_CODE.maxRange,
|
|
320
|
+
schema: schema,
|
|
321
|
+
subject: subject,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { EitherError } from './utils/fp';
|
|
3
|
+
import type { BaseSchema, Con_Schema_SubjT_V } from './types/compound-schema-types';
|
|
4
|
+
import type { BaseSchemaValidateError } from './error';
|
|
5
|
+
export type BaseSchemaSubjectType = string | number | boolean | Buffer | undefined;
|
|
6
|
+
export declare function validateBaseSchemaSubject<T extends BaseSchema>(schema: T, schemaSubject: unknown): EitherError<BaseSchemaValidateError, Con_Schema_SubjT_V<T>>;
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateBaseSchemaSubject = void 0;
|
|
4
|
+
var fp_1 = require("./utils/fp");
|
|
5
|
+
var error_1 = require("./error");
|
|
6
|
+
function validateBaseSchemaSubject(schema, subject) {
|
|
7
|
+
if (typeof schema === 'string') {
|
|
8
|
+
switch (schema) {
|
|
9
|
+
case 'string?':
|
|
10
|
+
case 'string': {
|
|
11
|
+
if (typeof subject !== 'string') {
|
|
12
|
+
if (subject === undefined && schema === 'string?') {
|
|
13
|
+
return (0, fp_1.data)(undefined);
|
|
14
|
+
}
|
|
15
|
+
return (0, fp_1.error)({
|
|
16
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
17
|
+
schema: schema,
|
|
18
|
+
subject: subject,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return (0, fp_1.data)(subject);
|
|
22
|
+
}
|
|
23
|
+
case 'number?':
|
|
24
|
+
case 'number': {
|
|
25
|
+
if (typeof subject !== 'number') {
|
|
26
|
+
if (subject === undefined && schema === 'number?') {
|
|
27
|
+
return (0, fp_1.data)(undefined);
|
|
28
|
+
}
|
|
29
|
+
return (0, fp_1.error)({
|
|
30
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
31
|
+
schema: schema,
|
|
32
|
+
subject: subject,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
if (Number.isNaN(subject)) {
|
|
36
|
+
return (0, fp_1.error)({
|
|
37
|
+
code: error_1.VALIDATE_ERROR_CODE.NaN,
|
|
38
|
+
schema: schema,
|
|
39
|
+
subject: subject,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if (Number.isFinite(subject) === false) {
|
|
43
|
+
return (0, fp_1.error)({
|
|
44
|
+
code: error_1.VALIDATE_ERROR_CODE.infinity,
|
|
45
|
+
schema: schema,
|
|
46
|
+
subject: subject,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return (0, fp_1.data)(subject);
|
|
50
|
+
}
|
|
51
|
+
case 'boolean?':
|
|
52
|
+
case 'boolean': {
|
|
53
|
+
if (typeof subject !== 'boolean') {
|
|
54
|
+
if (subject === undefined && schema === 'boolean?') {
|
|
55
|
+
return (0, fp_1.data)(undefined);
|
|
56
|
+
}
|
|
57
|
+
return (0, fp_1.error)({
|
|
58
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
59
|
+
schema: schema,
|
|
60
|
+
subject: subject,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return (0, fp_1.data)(subject);
|
|
64
|
+
}
|
|
65
|
+
case 'buffer?':
|
|
66
|
+
case 'buffer': {
|
|
67
|
+
if (Buffer.isBuffer(subject) === false) {
|
|
68
|
+
if (subject === undefined && schema === 'buffer?') {
|
|
69
|
+
return (0, fp_1.data)(undefined);
|
|
70
|
+
}
|
|
71
|
+
return (0, fp_1.error)({
|
|
72
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
73
|
+
schema: schema,
|
|
74
|
+
subject: subject,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return (0, fp_1.data)(subject);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
switch (schema.type) {
|
|
82
|
+
case 'string': {
|
|
83
|
+
if (typeof subject !== 'string') {
|
|
84
|
+
if (subject === undefined && schema.optional) {
|
|
85
|
+
return (0, fp_1.data)(undefined);
|
|
86
|
+
}
|
|
87
|
+
return (0, fp_1.error)({
|
|
88
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
89
|
+
schema: schema,
|
|
90
|
+
subject: subject,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (typeof schema.minLength === 'number') {
|
|
94
|
+
if (subject.length < schema.minLength) {
|
|
95
|
+
return (0, fp_1.error)({
|
|
96
|
+
code: error_1.VALIDATE_ERROR_CODE.minRange,
|
|
97
|
+
schema: schema,
|
|
98
|
+
subject: subject,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (typeof schema.maxLength === 'number') {
|
|
103
|
+
if (subject.length > schema.maxLength) {
|
|
104
|
+
return (0, fp_1.error)({
|
|
105
|
+
code: error_1.VALIDATE_ERROR_CODE.maxRange,
|
|
106
|
+
schema: schema,
|
|
107
|
+
subject: subject,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return (0, fp_1.data)(subject);
|
|
112
|
+
}
|
|
113
|
+
case 'number': {
|
|
114
|
+
if (typeof subject !== 'number') {
|
|
115
|
+
if (subject === undefined && schema.optional) {
|
|
116
|
+
return (0, fp_1.data)(undefined);
|
|
117
|
+
}
|
|
118
|
+
return (0, fp_1.error)({
|
|
119
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
120
|
+
schema: schema,
|
|
121
|
+
subject: subject,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
if (Number.isNaN(subject)) {
|
|
125
|
+
return (0, fp_1.error)({
|
|
126
|
+
code: error_1.VALIDATE_ERROR_CODE.NaN,
|
|
127
|
+
schema: schema,
|
|
128
|
+
subject: subject,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (Number.isFinite(subject) === false) {
|
|
132
|
+
return (0, fp_1.error)({
|
|
133
|
+
code: error_1.VALIDATE_ERROR_CODE.infinity,
|
|
134
|
+
schema: schema,
|
|
135
|
+
subject: subject,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if (typeof schema.min === 'number') {
|
|
139
|
+
if (subject < schema.min) {
|
|
140
|
+
return (0, fp_1.error)({
|
|
141
|
+
code: error_1.VALIDATE_ERROR_CODE.minRange,
|
|
142
|
+
schema: schema,
|
|
143
|
+
subject: subject,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (typeof schema.max === 'number') {
|
|
148
|
+
if (subject > schema.max) {
|
|
149
|
+
return (0, fp_1.error)({
|
|
150
|
+
code: error_1.VALIDATE_ERROR_CODE.maxRange,
|
|
151
|
+
schema: schema,
|
|
152
|
+
subject: subject,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return (0, fp_1.data)(subject);
|
|
157
|
+
}
|
|
158
|
+
case 'boolean': {
|
|
159
|
+
if (typeof subject !== 'boolean') {
|
|
160
|
+
if (subject === undefined && schema.optional) {
|
|
161
|
+
return (0, fp_1.data)(undefined);
|
|
162
|
+
}
|
|
163
|
+
return (0, fp_1.error)({
|
|
164
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
165
|
+
schema: schema,
|
|
166
|
+
subject: subject,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return (0, fp_1.data)(subject);
|
|
170
|
+
}
|
|
171
|
+
case 'buffer': {
|
|
172
|
+
if (Buffer.isBuffer(subject) === false) {
|
|
173
|
+
if (subject === undefined && schema.optional) {
|
|
174
|
+
return (0, fp_1.data)(undefined);
|
|
175
|
+
}
|
|
176
|
+
return (0, fp_1.error)({
|
|
177
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
178
|
+
schema: schema,
|
|
179
|
+
subject: subject,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
if (typeof schema.minLength === 'number') {
|
|
183
|
+
if (subject.length < schema.minLength) {
|
|
184
|
+
return (0, fp_1.error)({
|
|
185
|
+
code: error_1.VALIDATE_ERROR_CODE.minRange,
|
|
186
|
+
schema: schema,
|
|
187
|
+
subject: subject,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (typeof schema.maxLength === 'number') {
|
|
192
|
+
if (subject.length > schema.maxLength) {
|
|
193
|
+
return (0, fp_1.error)({
|
|
194
|
+
code: error_1.VALIDATE_ERROR_CODE.maxRange,
|
|
195
|
+
schema: schema,
|
|
196
|
+
subject: subject,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return (0, fp_1.data)(subject);
|
|
201
|
+
}
|
|
202
|
+
case 'stringUnion': {
|
|
203
|
+
if (typeof subject !== 'string') {
|
|
204
|
+
if (subject === undefined && schema.optional) {
|
|
205
|
+
return (0, fp_1.data)(undefined);
|
|
206
|
+
}
|
|
207
|
+
return (0, fp_1.error)({
|
|
208
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
209
|
+
schema: schema,
|
|
210
|
+
subject: subject,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
var unionSet = new Set(schema.of);
|
|
214
|
+
if (unionSet.has(subject) === false) {
|
|
215
|
+
return (0, fp_1.error)({
|
|
216
|
+
code: error_1.VALIDATE_ERROR_CODE.notInUnion,
|
|
217
|
+
schema: schema,
|
|
218
|
+
subject: subject,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
return (0, fp_1.data)(subject);
|
|
222
|
+
}
|
|
223
|
+
case 'numberUnion': {
|
|
224
|
+
if (typeof subject !== 'number') {
|
|
225
|
+
if (subject === undefined && schema.optional) {
|
|
226
|
+
return (0, fp_1.data)(undefined);
|
|
227
|
+
}
|
|
228
|
+
return (0, fp_1.error)({
|
|
229
|
+
code: error_1.VALIDATE_ERROR_CODE.invalidType,
|
|
230
|
+
schema: schema,
|
|
231
|
+
subject: subject,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
var unionSet = new Set(schema.of);
|
|
235
|
+
if (unionSet.has(subject) === false) {
|
|
236
|
+
return (0, fp_1.error)({
|
|
237
|
+
code: error_1.VALIDATE_ERROR_CODE.notInUnion,
|
|
238
|
+
schema: schema,
|
|
239
|
+
subject: subject,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
return (0, fp_1.data)(subject);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
exports.validateBaseSchemaSubject = validateBaseSchemaSubject;
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Schema, BaseSchema } from './types/compound-schema-types';
|
|
2
|
+
export type ParseErrorCode = (typeof PARSE_ERROR_CODE)[keyof typeof PARSE_ERROR_CODE];
|
|
3
|
+
export type ValidateErrorCode = (typeof VALIDATE_ERROR_CODE)[keyof typeof VALIDATE_ERROR_CODE];
|
|
4
|
+
export type BaseSchemaParseError = {
|
|
5
|
+
code: ParseErrorCode;
|
|
6
|
+
schema: BaseSchema;
|
|
7
|
+
subject: unknown;
|
|
8
|
+
};
|
|
9
|
+
export type BaseSchemaValidateError = {
|
|
10
|
+
code: ValidateErrorCode;
|
|
11
|
+
schema: BaseSchema;
|
|
12
|
+
subject: unknown;
|
|
13
|
+
};
|
|
14
|
+
export type ErrorPath = Array<string | number>;
|
|
15
|
+
export type ParseError = {
|
|
16
|
+
path: ErrorPath;
|
|
17
|
+
schema: Schema;
|
|
18
|
+
subject: unknown;
|
|
19
|
+
};
|
|
20
|
+
export type ValidateError = {
|
|
21
|
+
path: ErrorPath;
|
|
22
|
+
schema: Schema;
|
|
23
|
+
subject: unknown;
|
|
24
|
+
};
|
|
25
|
+
export declare const PARSE_ERROR_CODE: {
|
|
26
|
+
readonly invalidType: "INVALID_TYPE";
|
|
27
|
+
readonly NaN: "NOT_A_NUMBER";
|
|
28
|
+
readonly infinity: "INFINITY";
|
|
29
|
+
readonly minRange: "MIN_RANGE";
|
|
30
|
+
readonly maxRange: "MAX_RANGE";
|
|
31
|
+
readonly notInUnion: "NOT_IN_UNION";
|
|
32
|
+
readonly schemaDefaultMinRange: "SCHEMA_DEFAULT_MIN_RANGE";
|
|
33
|
+
readonly schemaDefaultMaxRange: "SCHEMA_DEFAULT_MAX_RANGE";
|
|
34
|
+
readonly schemaDefaultNotInUnion: "SCHEMA_DEFAULT_NOT_IN_UNION";
|
|
35
|
+
};
|
|
36
|
+
export declare const VALIDATE_ERROR_CODE: {
|
|
37
|
+
readonly invalidType: "INVALID_TYPE";
|
|
38
|
+
readonly NaN: "NOT_A_NUMBER";
|
|
39
|
+
readonly infinity: "INFINITY";
|
|
40
|
+
readonly minRange: "MIN_RANGE";
|
|
41
|
+
readonly maxRange: "MAX_RANGE";
|
|
42
|
+
readonly notInUnion: "NOT_IN_UNION";
|
|
43
|
+
};
|
|
44
|
+
export declare const PROGRAMMATICALLY_DEFINED_ERROR_MSG: {
|
|
45
|
+
readonly optionalDefined: "Schema \"optional\" is already defined";
|
|
46
|
+
readonly brandDefined: "Schema \"brand\" is already defined";
|
|
47
|
+
readonly minDefined: "Schema \"min\" is already defined";
|
|
48
|
+
readonly maxDefined: "Schema \"max\" is already defined";
|
|
49
|
+
readonly minLengthDefined: "Schema \"minLength\" is already defined";
|
|
50
|
+
readonly maxLengthDefined: "Schema \"maxLength\" is already defined";
|
|
51
|
+
readonly descriptionDefined: "Schema \"description\" is already defined";
|
|
52
|
+
readonly defaultDefined: "Schema \"default\" is already defined";
|
|
53
|
+
readonly defaultNotAllowed: "Schema \"default\" is allowed only for optional properties";
|
|
54
|
+
};
|
package/dist/error.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PROGRAMMATICALLY_DEFINED_ERROR_MSG = exports.VALIDATE_ERROR_CODE = exports.PARSE_ERROR_CODE = void 0;
|
|
4
|
+
exports.PARSE_ERROR_CODE = {
|
|
5
|
+
invalidType: 'INVALID_TYPE',
|
|
6
|
+
NaN: 'NOT_A_NUMBER',
|
|
7
|
+
infinity: 'INFINITY',
|
|
8
|
+
minRange: 'MIN_RANGE',
|
|
9
|
+
maxRange: 'MAX_RANGE',
|
|
10
|
+
notInUnion: 'NOT_IN_UNION',
|
|
11
|
+
schemaDefaultMinRange: 'SCHEMA_DEFAULT_MIN_RANGE',
|
|
12
|
+
schemaDefaultMaxRange: 'SCHEMA_DEFAULT_MAX_RANGE',
|
|
13
|
+
schemaDefaultNotInUnion: 'SCHEMA_DEFAULT_NOT_IN_UNION',
|
|
14
|
+
};
|
|
15
|
+
exports.VALIDATE_ERROR_CODE = {
|
|
16
|
+
invalidType: 'INVALID_TYPE',
|
|
17
|
+
NaN: 'NOT_A_NUMBER',
|
|
18
|
+
infinity: 'INFINITY',
|
|
19
|
+
minRange: 'MIN_RANGE',
|
|
20
|
+
maxRange: 'MAX_RANGE',
|
|
21
|
+
notInUnion: 'NOT_IN_UNION',
|
|
22
|
+
};
|
|
23
|
+
exports.PROGRAMMATICALLY_DEFINED_ERROR_MSG = {
|
|
24
|
+
optionalDefined: 'Schema "optional" is already defined',
|
|
25
|
+
brandDefined: 'Schema "brand" is already defined',
|
|
26
|
+
minDefined: 'Schema "min" is already defined',
|
|
27
|
+
maxDefined: 'Schema "max" is already defined',
|
|
28
|
+
minLengthDefined: 'Schema "minLength" is already defined',
|
|
29
|
+
maxLengthDefined: 'Schema "maxLength" is already defined',
|
|
30
|
+
descriptionDefined: 'Schema "description" is already defined',
|
|
31
|
+
defaultDefined: 'Schema "default" is already defined',
|
|
32
|
+
defaultNotAllowed: 'Schema "default" is allowed only for optional properties',
|
|
33
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { EitherError } from './utils/fp';
|
|
2
|
+
import type { Schema, Con_Schema_SubjT_P } from './types/compound-schema-types';
|
|
3
|
+
import type { ParseError } from './error';
|
|
4
|
+
export declare function parse<T extends Schema>(schema: T, subject: unknown): EitherError<ParseError[], Con_Schema_SubjT_P<T>>;
|