schematox 0.0.2 → 0.0.3
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/README.md +1 -24
- package/dist/base-schema-parser.d.ts +1 -2
- package/dist/base-schema-parser.js +0 -49
- package/dist/base-schema-validator.d.ts +1 -2
- package/dist/base-schema-validator.js +0 -45
- package/dist/general-schema-parser.js +0 -1
- package/dist/general-schema-validator.js +0 -1
- package/dist/index.js +1 -3
- package/dist/index.ts +0 -1
- package/dist/types/base-detailed-schema-types.ts +5 -19
- package/dist/types/base-short-schema-types.ts +4 -19
- package/package.json +1 -3
package/README.md
CHANGED
|
@@ -169,7 +169,6 @@ import {
|
|
|
169
169
|
string,
|
|
170
170
|
number,
|
|
171
171
|
boolean,
|
|
172
|
-
buffer,
|
|
173
172
|
stringUnion,
|
|
174
173
|
numberUnion,
|
|
175
174
|
} from 'schematox'
|
|
@@ -243,28 +242,6 @@ const programmaticBoolean = boolean()
|
|
|
243
242
|
.brand('x', 'y')
|
|
244
243
|
.description('y')
|
|
245
244
|
|
|
246
|
-
// buffer
|
|
247
|
-
// The default value is not supported because Buffer is not compatible with JSON
|
|
248
|
-
|
|
249
|
-
const staticShortBufferRequired = 'buffer' satisfies Schema
|
|
250
|
-
const staticShortBufferOptional = 'buffer?' satisfies Schema
|
|
251
|
-
|
|
252
|
-
const staticDetailedBuffer = {
|
|
253
|
-
type: 'buffer',
|
|
254
|
-
optional: true,
|
|
255
|
-
brand: ['x', 'y'],
|
|
256
|
-
minLength: 1,
|
|
257
|
-
maxLength: 1,
|
|
258
|
-
description: 'x',
|
|
259
|
-
} as const satisfies Schema
|
|
260
|
-
|
|
261
|
-
const programmaticBuffer = buffer()
|
|
262
|
-
.optional()
|
|
263
|
-
.minLength(1)
|
|
264
|
-
.maxLength(1)
|
|
265
|
-
.brand('x', 'y')
|
|
266
|
-
.description('y')
|
|
267
|
-
|
|
268
245
|
// stringUnion
|
|
269
246
|
|
|
270
247
|
const staticStringUnion = {
|
|
@@ -359,7 +336,7 @@ The `result.error` will be:
|
|
|
359
336
|
|
|
360
337
|
## Parse/validate differences
|
|
361
338
|
|
|
362
|
-
The parser provides a new object/primitive without references to the evaluated subject
|
|
339
|
+
The parser provides a new object/primitive without references to the evaluated subject. This functionality is responsible for clearing the `array` schema result value from `undefined` optional schema values.
|
|
363
340
|
|
|
364
341
|
Moreover, the parser manages the `null` value as `undefined` and subsequently replaces it with `undefined`. It also swaps `optional` values with the `default` value, provided that the default values are explicitly stated in the schema.
|
|
365
342
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import type { EitherError } from './utils/fp';
|
|
3
2
|
import type { BaseSchema, Con_Schema_SubjT_P } from './types/compound-schema-types';
|
|
4
3
|
import type { BaseSchemaParseError } from './error';
|
|
5
|
-
export type BaseSchemaSubjectType = string | number | boolean |
|
|
4
|
+
export type BaseSchemaSubjectType = string | number | boolean | undefined;
|
|
6
5
|
export declare function parseBaseSchemaSubject<T extends BaseSchema>(schema: T, schemaSubject: unknown): EitherError<BaseSchemaParseError, Con_Schema_SubjT_P<T>>;
|
|
@@ -68,22 +68,6 @@ function parseBaseSchemaSubject(schema, subject) {
|
|
|
68
68
|
}
|
|
69
69
|
return (0, fp_1.data)(subject);
|
|
70
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
71
|
}
|
|
88
72
|
}
|
|
89
73
|
switch (schema.type) {
|
|
@@ -171,39 +155,6 @@ function parseBaseSchemaSubject(schema, subject) {
|
|
|
171
155
|
}
|
|
172
156
|
return (0, fp_1.data)(subject);
|
|
173
157
|
}
|
|
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
158
|
case 'stringUnion': {
|
|
208
159
|
if (typeof subject !== 'string') {
|
|
209
160
|
if (subject === undefined || subject === null) {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import type { EitherError } from './utils/fp';
|
|
3
2
|
import type { BaseSchema, Con_Schema_SubjT_V } from './types/compound-schema-types';
|
|
4
3
|
import type { BaseSchemaValidateError } from './error';
|
|
5
|
-
export type BaseSchemaSubjectType = string | number | boolean |
|
|
4
|
+
export type BaseSchemaSubjectType = string | number | boolean | undefined;
|
|
6
5
|
export declare function validateBaseSchemaSubject<T extends BaseSchema>(schema: T, schemaSubject: unknown): EitherError<BaseSchemaValidateError, Con_Schema_SubjT_V<T>>;
|
|
@@ -62,20 +62,6 @@ function validateBaseSchemaSubject(schema, subject) {
|
|
|
62
62
|
}
|
|
63
63
|
return (0, fp_1.data)(subject);
|
|
64
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
65
|
}
|
|
80
66
|
}
|
|
81
67
|
switch (schema.type) {
|
|
@@ -168,37 +154,6 @@ function validateBaseSchemaSubject(schema, subject) {
|
|
|
168
154
|
}
|
|
169
155
|
return (0, fp_1.data)(subject);
|
|
170
156
|
}
|
|
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
157
|
case 'stringUnion': {
|
|
203
158
|
if (typeof subject !== 'string') {
|
|
204
159
|
if (subject === undefined && schema.optional) {
|
|
@@ -45,7 +45,6 @@ function parse(schema, subject) {
|
|
|
45
45
|
schema.type === 'string' ||
|
|
46
46
|
schema.type === 'number' ||
|
|
47
47
|
schema.type === 'boolean' ||
|
|
48
|
-
schema.type === 'buffer' ||
|
|
49
48
|
schema.type === 'stringUnion' ||
|
|
50
49
|
schema.type === 'numberUnion') {
|
|
51
50
|
var parsed = (0, base_schema_parser_1.parseBaseSchemaSubject)(schema, subject);
|
|
@@ -45,7 +45,6 @@ function validate(schema, subject) {
|
|
|
45
45
|
schema.type === 'string' ||
|
|
46
46
|
schema.type === 'number' ||
|
|
47
47
|
schema.type === 'boolean' ||
|
|
48
|
-
schema.type === 'buffer' ||
|
|
49
48
|
schema.type === 'stringUnion' ||
|
|
50
49
|
schema.type === 'numberUnion') {
|
|
51
50
|
var validated = (0, base_schema_validator_1.validateBaseSchemaSubject)(schema, subject);
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* but only with a MINOR version update.
|
|
9
9
|
**/
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.x = exports.validate = exports.parse = exports.object = exports.array = exports.numberUnion = exports.stringUnion = exports.
|
|
11
|
+
exports.x = exports.validate = exports.parse = exports.object = exports.array = exports.numberUnion = exports.stringUnion = exports.boolean = exports.number = exports.string = void 0;
|
|
12
12
|
/* Programmatically base schema definition */
|
|
13
13
|
var string_1 = require("./programmatic-schema/string");
|
|
14
14
|
Object.defineProperty(exports, "string", { enumerable: true, get: function () { return string_1.string; } });
|
|
@@ -16,8 +16,6 @@ var number_1 = require("./programmatic-schema/number");
|
|
|
16
16
|
Object.defineProperty(exports, "number", { enumerable: true, get: function () { return number_1.number; } });
|
|
17
17
|
var boolean_1 = require("./programmatic-schema/boolean");
|
|
18
18
|
Object.defineProperty(exports, "boolean", { enumerable: true, get: function () { return boolean_1.boolean; } });
|
|
19
|
-
var buffer_1 = require("./programmatic-schema/buffer");
|
|
20
|
-
Object.defineProperty(exports, "buffer", { enumerable: true, get: function () { return buffer_1.buffer; } });
|
|
21
19
|
var string_union_1 = require("./programmatic-schema/string-union");
|
|
22
20
|
Object.defineProperty(exports, "stringUnion", { enumerable: true, get: function () { return string_union_1.stringUnion; } });
|
|
23
21
|
var number_union_1 = require("./programmatic-schema/number-union");
|
package/dist/index.ts
CHANGED
|
@@ -18,7 +18,6 @@ import type {
|
|
|
18
18
|
export { string } from './programmatic-schema/string'
|
|
19
19
|
export { number } from './programmatic-schema/number'
|
|
20
20
|
export { boolean } from './programmatic-schema/boolean'
|
|
21
|
-
export { buffer } from './programmatic-schema/buffer'
|
|
22
21
|
export { stringUnion } from './programmatic-schema/string-union'
|
|
23
22
|
export { numberUnion } from './programmatic-schema/number-union'
|
|
24
23
|
|
|
@@ -36,17 +36,6 @@ export type BD_Boolean = {
|
|
|
36
36
|
brand?: BrandSchema
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
export type BD_Buffer = {
|
|
40
|
-
type: 'buffer'
|
|
41
|
-
optional?: boolean
|
|
42
|
-
|
|
43
|
-
description?: string
|
|
44
|
-
brand?: BrandSchema
|
|
45
|
-
|
|
46
|
-
minLength?: number /* >= */
|
|
47
|
-
maxLength?: number /* <= */
|
|
48
|
-
}
|
|
49
|
-
|
|
50
39
|
export type BD_StringUnion<T extends string = string> = {
|
|
51
40
|
type: 'stringUnion'
|
|
52
41
|
of: Readonly<Array<T>>
|
|
@@ -71,7 +60,6 @@ export type BD_Schema =
|
|
|
71
60
|
| BD_String
|
|
72
61
|
| BD_Number
|
|
73
62
|
| BD_Boolean
|
|
74
|
-
| BD_Buffer
|
|
75
63
|
| BD_StringUnion
|
|
76
64
|
| BD_NumberUnion
|
|
77
65
|
|
|
@@ -82,13 +70,11 @@ export type Con_BD_Schema_TypeOnly_SubjT<T extends BD_Schema> =
|
|
|
82
70
|
? number
|
|
83
71
|
: T extends BD_Boolean
|
|
84
72
|
? boolean
|
|
85
|
-
: T extends
|
|
86
|
-
?
|
|
87
|
-
: T extends
|
|
88
|
-
?
|
|
89
|
-
:
|
|
90
|
-
? V
|
|
91
|
-
: never
|
|
73
|
+
: T extends BD_StringUnion<infer U>
|
|
74
|
+
? U
|
|
75
|
+
: T extends BD_NumberUnion<infer V>
|
|
76
|
+
? V
|
|
77
|
+
: never
|
|
92
78
|
|
|
93
79
|
export type Con_BrandSchema_SubjT<T extends BrandSchema> = T extends Readonly<
|
|
94
80
|
[infer U, infer V]
|
|
@@ -7,20 +7,9 @@ export type BS_Number_Opt = 'number?'
|
|
|
7
7
|
export type BS_Boolean_Req = 'boolean'
|
|
8
8
|
export type BS_Boolean_Opt = 'boolean?'
|
|
9
9
|
|
|
10
|
-
export type
|
|
11
|
-
export type BS_Buffer_Opt = 'buffer?'
|
|
10
|
+
export type BS_Schema_Req = BS_String_Req | BS_Number_Req | BS_Boolean_Req
|
|
12
11
|
|
|
13
|
-
export type
|
|
14
|
-
| BS_String_Req
|
|
15
|
-
| BS_Number_Req
|
|
16
|
-
| BS_Boolean_Req
|
|
17
|
-
| BS_Buffer_Req
|
|
18
|
-
|
|
19
|
-
export type BS_Schema_Opt =
|
|
20
|
-
| BS_String_Opt
|
|
21
|
-
| BS_Number_Opt
|
|
22
|
-
| BS_Boolean_Opt
|
|
23
|
-
| BS_Buffer_Opt
|
|
12
|
+
export type BS_Schema_Opt = BS_String_Opt | BS_Number_Opt | BS_Boolean_Opt
|
|
24
13
|
|
|
25
14
|
export type BS_Schema = BS_Schema_Req | BS_Schema_Opt
|
|
26
15
|
|
|
@@ -31,9 +20,7 @@ export type Con_BS_Schema_Req_SubjT<T extends BS_Schema_Req> =
|
|
|
31
20
|
? number
|
|
32
21
|
: T extends BS_Boolean_Req
|
|
33
22
|
? boolean
|
|
34
|
-
:
|
|
35
|
-
? Buffer
|
|
36
|
-
: never
|
|
23
|
+
: never
|
|
37
24
|
|
|
38
25
|
export type Con_BS_Schema_Opt_SubjT<T extends BS_Schema_Opt> =
|
|
39
26
|
T extends BS_String_Opt
|
|
@@ -42,9 +29,7 @@ export type Con_BS_Schema_Opt_SubjT<T extends BS_Schema_Opt> =
|
|
|
42
29
|
? number | undefined
|
|
43
30
|
: T extends BS_Boolean_Opt
|
|
44
31
|
? boolean | undefined
|
|
45
|
-
:
|
|
46
|
-
? Buffer | undefined
|
|
47
|
-
: never
|
|
32
|
+
: never
|
|
48
33
|
|
|
49
34
|
export type Con_BS_Schema_SubjT<T extends BS_Schema> = T extends BS_Schema_Req
|
|
50
35
|
? Con_BS_Schema_Req_SubjT<T>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "schematox",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Define JSON compatible schema statically/programmatically and parse/validate its subject with typesafety",
|
|
5
5
|
"author": "Konstantin Mazur",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,7 +34,6 @@
|
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/jest": "^29.5.1",
|
|
37
|
-
"@types/node": "^20.1.4",
|
|
38
37
|
"@typescript-eslint/eslint-plugin": "^5.59.5",
|
|
39
38
|
"@typescript-eslint/parser": "^5.59.5",
|
|
40
39
|
"eslint": "^8.40.0",
|
|
@@ -42,7 +41,6 @@
|
|
|
42
41
|
"jest": "^29.5.0",
|
|
43
42
|
"prettier": "^3.1.1",
|
|
44
43
|
"ts-jest": "^29.1.0",
|
|
45
|
-
"ts-node": "^10.9.1",
|
|
46
44
|
"typescript": "^5.0.4"
|
|
47
45
|
}
|
|
48
46
|
}
|