schematox 0.0.5 → 0.1.0

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 CHANGED
@@ -75,7 +75,7 @@ export const userSchema = {
75
75
  },
76
76
  minLength: 1,
77
77
  },
78
- bio: 'string?',
78
+ bio: { type: 'string', optional: true },
79
79
  },
80
80
  } as const satisfies Schema
81
81
  ```
@@ -179,10 +179,7 @@ import type { Schema } from 'schematox'
179
179
 
180
180
  // string
181
181
 
182
- const staticShortStringRequired = 'string' satisfies Schema
183
- const staticShortStringOptional = 'string?' satisfies Schema
184
-
185
- const staticDetailedString = {
182
+ const staticString = {
186
183
  type: 'string',
187
184
  optional: true,
188
185
  default: 'x',
@@ -202,10 +199,7 @@ const programmaticString = string()
202
199
 
203
200
  // number
204
201
 
205
- const staticShortNumberRequired = 'number' satisfies Schema
206
- const staticShortNumberOptional = 'number?' satisfies Schema
207
-
208
- const staticDetailedNumber = {
202
+ const staticNumber = {
209
203
  type: 'number',
210
204
  optional: true,
211
205
  default: 1,
@@ -225,10 +219,7 @@ const programmaticNumber = number()
225
219
 
226
220
  // boolean
227
221
 
228
- const staticShortBooleanRequired = 'boolean' satisfies Schema
229
- const staticShortBooleanOptional = 'boolean?' satisfies Schema
230
-
231
- const staticDetailedBoolean = {
222
+ const staticBoolean = {
232
223
  type: 'boolean',
233
224
  optional: true,
234
225
  default: false,
@@ -321,7 +312,7 @@ const schemaX = x(
321
312
  const result = schemaX.parse({ x: { y: [{ z: 0 }] } })
322
313
  ```
323
314
 
324
- The `result.error` will be:
315
+ The `result.error` shape is:
325
316
 
326
317
  ```json
327
318
  [
@@ -334,14 +325,14 @@ The `result.error` will be:
334
325
  ]
335
326
  ```
336
327
 
337
- If the `error` is present it's always an array which has at least one entry. Each entry has the following properties:
328
+ It's always an array with at least one entry. Each entry includes:
338
329
 
339
- - `code` could be `INVALID_TYPE` if schema subject or schema default value is not satisfies schema type requirement. Another code `INVALID_RANGE` which is about `min/max` and `minLength/maxLength` schema requirements
340
- - `schema` the particular chunk of the `schema` where invalid subject value is found
341
- - `subject` – the particular chunk of the validated subject where invalid value is found
342
- - `path` – the path to error subject chunk from the root of the evaluated subject. Strings is keys and numbers are the array indexes
330
+ - `code`: Specifies either `INVALID_TYPE` (when schema subject or default value don't meet schema type specifications), or `INVALID_RANGE` (when `min/max` or `minLength/maxLength` schema requirements aren't met).
331
+ - `schema`: The specific section of `schema` where the invalid value is found.
332
+ - `subject`: The specific part of the validated subject where the invalid value exists.
333
+ - `path`: Traces the route from the root to the error subject, with strings as keys and numbers as array indexes.
343
334
 
344
- ## Parse/validate differences
335
+ ## Parse and validate differences
345
336
 
346
337
  The parser returns `data` as new object/primitive without references to the parsed subject. Parser manages the `null` value as `undefined` and subsequently replaces it with `undefined`. It also swaps `optional` values with the `default` value from schema. One can infer schema parsed subject type by using `XParsed<typeof schema>` generic.
347
338
 
@@ -352,14 +343,14 @@ So the difference between `XParsed` and `XValidated` is just about handling `def
352
343
  Examples:
353
344
 
354
345
  ```typescript
355
- const optionalStrX = x('string?')
346
+ const optionalStrX = x({ type: 'string', optional: true } as const)
356
347
 
357
348
  /* Parser doesn't check subject type */
358
349
 
359
350
  expect(optionalStrX.parse(0).error).toStrictEqual([
360
351
  {
361
352
  code: 'INVALID_TYPE',
362
- schema: 'string?',
353
+ schema: { type: 'string', optional: true },
363
354
  subject: 0,
364
355
  path: [],
365
356
  },
@@ -371,7 +362,7 @@ expect(optionalStrX.parse(0).error).toStrictEqual([
371
362
  expect(optionalStrX.validate(0).error).toStrictEqual([
372
363
  {
373
364
  code: 'INVALID_TYPE',
374
- schema: 'string?',
365
+ schema: { type: 'string', optional: true },
375
366
  subject: 0,
376
367
  path: [],
377
368
  },
@@ -388,7 +379,7 @@ expect(optionalStrX.parse(null).error).toBe(undefined)
388
379
  expect(optionalStrX.validate(null).error).toStrictEqual([
389
380
  {
390
381
  code: 'INVALID_TYPE',
391
- schema: 'string?',
382
+ schema: { type: 'string', optional: true },
392
383
  subject: null,
393
384
  path: [],
394
385
  },
@@ -4,69 +4,6 @@ exports.parseBaseSchemaSubject = void 0;
4
4
  var fp_1 = require("./utils/fp");
5
5
  var error_1 = require("./error");
6
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.ERROR_CODE.invalidType,
19
- path: this || [],
20
- schema: schema,
21
- subject: subject,
22
- });
23
- }
24
- return (0, fp_1.data)(subject);
25
- }
26
- case 'number?':
27
- case 'number': {
28
- if (typeof subject !== 'number') {
29
- if (subject === null || subject === undefined) {
30
- if (schema === 'number?') {
31
- return (0, fp_1.data)(undefined);
32
- }
33
- }
34
- return (0, fp_1.error)({
35
- code: error_1.ERROR_CODE.invalidType,
36
- path: this || [],
37
- schema: schema,
38
- subject: subject,
39
- });
40
- }
41
- if (Number.isFinite(subject) === false) {
42
- return (0, fp_1.error)({
43
- code: error_1.ERROR_CODE.invalidType,
44
- path: this || [],
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 === null || subject === undefined) {
55
- if (schema === 'boolean?') {
56
- return (0, fp_1.data)(undefined);
57
- }
58
- }
59
- return (0, fp_1.error)({
60
- code: error_1.ERROR_CODE.invalidType,
61
- path: this || [],
62
- schema: schema,
63
- subject: subject,
64
- });
65
- }
66
- return (0, fp_1.data)(subject);
67
- }
68
- }
69
- }
70
7
  var updatedSubject = typeof schema.default !== undefined &&
71
8
  schema.optional &&
72
9
  (subject === null || subject === undefined)
@@ -4,63 +4,6 @@ exports.validateBaseSchemaSubject = void 0;
4
4
  var fp_1 = require("./utils/fp");
5
5
  var error_1 = require("./error");
6
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.ERROR_CODE.invalidType,
17
- path: this || [],
18
- schema: schema,
19
- subject: subject,
20
- });
21
- }
22
- return (0, fp_1.data)(subject);
23
- }
24
- case 'number?':
25
- case 'number': {
26
- if (typeof subject !== 'number') {
27
- if (subject === undefined && schema === 'number?') {
28
- return (0, fp_1.data)(undefined);
29
- }
30
- return (0, fp_1.error)({
31
- code: error_1.ERROR_CODE.invalidType,
32
- path: this || [],
33
- schema: schema,
34
- subject: subject,
35
- });
36
- }
37
- if (Number.isFinite(subject) === false) {
38
- return (0, fp_1.error)({
39
- code: error_1.ERROR_CODE.invalidType,
40
- path: this || [],
41
- schema: schema,
42
- subject: subject,
43
- });
44
- }
45
- return (0, fp_1.data)(subject);
46
- }
47
- case 'boolean?':
48
- case 'boolean': {
49
- if (typeof subject !== 'boolean') {
50
- if (subject === undefined && schema === 'boolean?') {
51
- return (0, fp_1.data)(undefined);
52
- }
53
- return (0, fp_1.error)({
54
- code: error_1.ERROR_CODE.invalidType,
55
- path: this || [],
56
- schema: schema,
57
- subject: subject,
58
- });
59
- }
60
- return (0, fp_1.data)(subject);
61
- }
62
- }
63
- }
64
7
  switch (schema.type) {
65
8
  case 'string': {
66
9
  if (typeof subject !== 'string') {
@@ -30,8 +30,7 @@ var fp_1 = require("./utils/fp");
30
30
  var error_1 = require("./error");
31
31
  var base_schema_parser_1 = require("./base-schema-parser");
32
32
  function parse(schema, subject) {
33
- if (typeof schema === 'string' ||
34
- schema.type === 'string' ||
33
+ if (schema.type === 'string' ||
35
34
  schema.type === 'number' ||
36
35
  schema.type === 'boolean' ||
37
36
  schema.type === 'stringUnion' ||
@@ -30,8 +30,7 @@ var fp_1 = require("./utils/fp");
30
30
  var error_1 = require("./error");
31
31
  var base_schema_validator_1 = require("./base-schema-validator");
32
32
  function validate(schema, subject) {
33
- if (typeof schema === 'string' ||
34
- schema.type === 'string' ||
33
+ if (schema.type === 'string' ||
35
34
  schema.type === 'number' ||
36
35
  schema.type === 'boolean' ||
37
36
  schema.type === 'stringUnion' ||
package/dist/index.js CHANGED
@@ -8,8 +8,8 @@
8
8
  * but only with a MINOR version update.
9
9
  **/
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.data = exports.error = exports.isData = exports.isError = exports.x = exports.validate = exports.parse = exports.object = exports.array = exports.numberUnion = exports.stringUnion = exports.boolean = exports.number = exports.string = void 0;
12
- /* Programmatically base schema definition */
11
+ exports.x = exports.validate = exports.parse = exports.data = exports.error = exports.isData = exports.isError = exports.object = exports.array = exports.numberUnion = exports.stringUnion = exports.boolean = exports.number = exports.string = void 0;
12
+ /* Programmatic 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; } });
15
15
  var number_1 = require("./programmatic-schema/number");
@@ -20,20 +20,19 @@ var string_union_1 = require("./programmatic-schema/string-union");
20
20
  Object.defineProperty(exports, "stringUnion", { enumerable: true, get: function () { return string_union_1.stringUnion; } });
21
21
  var number_union_1 = require("./programmatic-schema/number-union");
22
22
  Object.defineProperty(exports, "numberUnion", { enumerable: true, get: function () { return number_union_1.numberUnion; } });
23
- /* Programmatically compound schema definition */
23
+ /* Programmatic compound schema definition */
24
24
  var array_1 = require("./programmatic-schema/array");
25
25
  Object.defineProperty(exports, "array", { enumerable: true, get: function () { return array_1.array; } });
26
26
  var object_1 = require("./programmatic-schema/object");
27
27
  Object.defineProperty(exports, "object", { enumerable: true, get: function () { return object_1.object; } });
28
- /* Parser/validator/x-closure */
28
+ var fp_1 = require("./utils/fp");
29
+ Object.defineProperty(exports, "isError", { enumerable: true, get: function () { return fp_1.isError; } });
30
+ Object.defineProperty(exports, "isData", { enumerable: true, get: function () { return fp_1.isData; } });
31
+ Object.defineProperty(exports, "error", { enumerable: true, get: function () { return fp_1.error; } });
32
+ Object.defineProperty(exports, "data", { enumerable: true, get: function () { return fp_1.data; } });
29
33
  var general_schema_parser_1 = require("./general-schema-parser");
30
34
  Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return general_schema_parser_1.parse; } });
31
35
  var general_schema_validator_1 = require("./general-schema-validator");
32
36
  Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return general_schema_validator_1.validate; } });
33
37
  var x_closure_1 = require("./x-closure");
34
38
  Object.defineProperty(exports, "x", { enumerable: true, get: function () { return x_closure_1.x; } });
35
- var fp_1 = require("./utils/fp");
36
- Object.defineProperty(exports, "isError", { enumerable: true, get: function () { return fp_1.isError; } });
37
- Object.defineProperty(exports, "isData", { enumerable: true, get: function () { return fp_1.isData; } });
38
- Object.defineProperty(exports, "error", { enumerable: true, get: function () { return fp_1.error; } });
39
- Object.defineProperty(exports, "data", { enumerable: true, get: function () { return fp_1.data; } });
package/dist/index.ts CHANGED
@@ -13,7 +13,7 @@ import type {
13
13
  Con_Schema_SubjT_V,
14
14
  } from './types/compound-schema-types'
15
15
 
16
- /* Programmatically base schema definition */
16
+ /* Programmatic base schema definition */
17
17
 
18
18
  export { string } from './programmatic-schema/string'
19
19
  export { number } from './programmatic-schema/number'
@@ -21,17 +21,11 @@ export { boolean } from './programmatic-schema/boolean'
21
21
  export { stringUnion } from './programmatic-schema/string-union'
22
22
  export { numberUnion } from './programmatic-schema/number-union'
23
23
 
24
- /* Programmatically compound schema definition */
24
+ /* Programmatic compound schema definition */
25
25
 
26
26
  export { array } from './programmatic-schema/array'
27
27
  export { object } from './programmatic-schema/object'
28
28
 
29
- /* Parser/validator/x-closure */
30
-
31
- export { parse } from './general-schema-parser'
32
- export { validate } from './general-schema-validator'
33
- export { x } from './x-closure'
34
-
35
29
  /* Typings */
36
30
 
37
31
  export type { BaseSchema, Schema } from './types/compound-schema-types'
@@ -42,6 +36,10 @@ export type { InvalidSubject } from './error'
42
36
  export type { EitherError } from './utils/fp'
43
37
  export { isError, isData, error, data } from './utils/fp'
44
38
 
39
+ export { parse } from './general-schema-parser'
40
+ export { validate } from './general-schema-validator'
41
+ export { x } from './x-closure'
42
+
45
43
  /* XParsed infers set optional default schema value as always present */
46
44
  export type XParsed<T extends { __schema: Schema } | Schema> = T extends {
47
45
  __schema: Schema
@@ -23,32 +23,32 @@ type BooleanRequired<T extends BD_Boolean> = Omit<{
23
23
  }>;
24
24
  } & BooleanShared<T>, keyof T>;
25
25
  export declare function boolean(): {
26
- optional: () => ExtWith_Option<{
26
+ brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
27
27
  readonly type: "boolean";
28
28
  }, {
29
- optional: true;
29
+ brand: readonly [U, V];
30
30
  }, Readonly<{
31
31
  readonly type: "boolean";
32
32
  } & {
33
- optional: true;
33
+ brand: readonly [U, V];
34
34
  }>>;
35
- description: (description: string) => ExtWith_Option<{
35
+ optional: () => ExtWith_Option<{
36
36
  readonly type: "boolean";
37
37
  }, {
38
- description: string;
38
+ optional: true;
39
39
  }, Readonly<{
40
40
  readonly type: "boolean";
41
41
  } & {
42
- description: string;
42
+ optional: true;
43
43
  }>>;
44
- brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
44
+ description: (description: string) => ExtWith_Option<{
45
45
  readonly type: "boolean";
46
46
  }, {
47
- brand: readonly [U, V];
47
+ description: string;
48
48
  }, Readonly<{
49
49
  readonly type: "boolean";
50
50
  } & {
51
- brand: readonly [U, V];
51
+ description: string;
52
52
  }>>;
53
53
  __schema: {
54
54
  readonly type: "boolean";
@@ -25,38 +25,38 @@ type NumberUnionRequired<T extends BD_NumberUnion> = Omit<{
25
25
  }>;
26
26
  } & NumberUnionShared<T>, keyof T>;
27
27
  export declare function numberUnion<T extends number>(...of: Readonly<[T, ...T[]]>): {
28
- optional: () => ExtWith_Option<{
28
+ brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
29
29
  readonly type: "numberUnion";
30
30
  readonly of: readonly [T, ...T[]];
31
31
  }, {
32
- optional: true;
32
+ brand: readonly [U, V];
33
33
  }, Readonly<{
34
34
  readonly type: "numberUnion";
35
35
  readonly of: readonly [T, ...T[]];
36
36
  } & {
37
- optional: true;
37
+ brand: readonly [U, V];
38
38
  }>>;
39
- description: (description: string) => ExtWith_Option<{
39
+ optional: () => ExtWith_Option<{
40
40
  readonly type: "numberUnion";
41
41
  readonly of: readonly [T, ...T[]];
42
42
  }, {
43
- description: string;
43
+ optional: true;
44
44
  }, Readonly<{
45
45
  readonly type: "numberUnion";
46
46
  readonly of: readonly [T, ...T[]];
47
47
  } & {
48
- description: string;
48
+ optional: true;
49
49
  }>>;
50
- brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
50
+ description: (description: string) => ExtWith_Option<{
51
51
  readonly type: "numberUnion";
52
52
  readonly of: readonly [T, ...T[]];
53
53
  }, {
54
- brand: readonly [U, V];
54
+ description: string;
55
55
  }, Readonly<{
56
56
  readonly type: "numberUnion";
57
57
  readonly of: readonly [T, ...T[]];
58
58
  } & {
59
- brand: readonly [U, V];
59
+ description: string;
60
60
  }>>;
61
61
  __schema: {
62
62
  readonly type: "numberUnion";
@@ -29,50 +29,50 @@ type NumberRequired<T extends BD_Number> = Omit<{
29
29
  }>;
30
30
  } & NumberShared<T>, keyof T>;
31
31
  export declare function number(): {
32
- optional: () => ExtWith_Option<{
32
+ brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
33
33
  readonly type: "number";
34
34
  }, {
35
- optional: true;
35
+ brand: readonly [U, V];
36
36
  }, Readonly<{
37
37
  readonly type: "number";
38
38
  } & {
39
- optional: true;
39
+ brand: readonly [U, V];
40
40
  }>>;
41
- description: (description: string) => ExtWith_Option<{
41
+ min: (min: number) => ExtWith_Option<{
42
42
  readonly type: "number";
43
43
  }, {
44
- description: string;
44
+ min: number;
45
45
  }, Readonly<{
46
46
  readonly type: "number";
47
47
  } & {
48
- description: string;
48
+ min: number;
49
49
  }>>;
50
- brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
50
+ max: (max: number) => ExtWith_Option<{
51
51
  readonly type: "number";
52
52
  }, {
53
- brand: readonly [U, V];
53
+ max: number;
54
54
  }, Readonly<{
55
55
  readonly type: "number";
56
56
  } & {
57
- brand: readonly [U, V];
57
+ max: number;
58
58
  }>>;
59
- min: (min: number) => ExtWith_Option<{
59
+ optional: () => ExtWith_Option<{
60
60
  readonly type: "number";
61
61
  }, {
62
- min: number;
62
+ optional: true;
63
63
  }, Readonly<{
64
64
  readonly type: "number";
65
65
  } & {
66
- min: number;
66
+ optional: true;
67
67
  }>>;
68
- max: (max: number) => ExtWith_Option<{
68
+ description: (description: string) => ExtWith_Option<{
69
69
  readonly type: "number";
70
70
  }, {
71
- max: number;
71
+ description: string;
72
72
  }, Readonly<{
73
73
  readonly type: "number";
74
74
  } & {
75
- max: number;
75
+ description: string;
76
76
  }>>;
77
77
  __schema: {
78
78
  readonly type: "number";
@@ -25,38 +25,38 @@ type StringUnionRequired<T extends BD_StringUnion> = Omit<{
25
25
  }>;
26
26
  } & StringUnionShared<T>, keyof T>;
27
27
  export declare function stringUnion<T extends string>(...of: Readonly<[T, ...T[]]>): {
28
- optional: () => ExtWith_Option<{
28
+ brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
29
29
  readonly type: "stringUnion";
30
30
  readonly of: readonly [T, ...T[]];
31
31
  }, {
32
- optional: true;
32
+ brand: readonly [U, V];
33
33
  }, Readonly<{
34
34
  readonly type: "stringUnion";
35
35
  readonly of: readonly [T, ...T[]];
36
36
  } & {
37
- optional: true;
37
+ brand: readonly [U, V];
38
38
  }>>;
39
- description: (description: string) => ExtWith_Option<{
39
+ optional: () => ExtWith_Option<{
40
40
  readonly type: "stringUnion";
41
41
  readonly of: readonly [T, ...T[]];
42
42
  }, {
43
- description: string;
43
+ optional: true;
44
44
  }, Readonly<{
45
45
  readonly type: "stringUnion";
46
46
  readonly of: readonly [T, ...T[]];
47
47
  } & {
48
- description: string;
48
+ optional: true;
49
49
  }>>;
50
- brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
50
+ description: (description: string) => ExtWith_Option<{
51
51
  readonly type: "stringUnion";
52
52
  readonly of: readonly [T, ...T[]];
53
53
  }, {
54
- brand: readonly [U, V];
54
+ description: string;
55
55
  }, Readonly<{
56
56
  readonly type: "stringUnion";
57
57
  readonly of: readonly [T, ...T[]];
58
58
  } & {
59
- brand: readonly [U, V];
59
+ description: string;
60
60
  }>>;
61
61
  __schema: {
62
62
  readonly type: "stringUnion";
@@ -29,50 +29,50 @@ type StringRequired<T extends BD_String> = Omit<{
29
29
  }>;
30
30
  } & StringShared<T>, keyof T>;
31
31
  export declare function string(): {
32
- optional: () => ExtWith_Option<{
32
+ brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
33
33
  readonly type: "string";
34
34
  }, {
35
- optional: true;
35
+ brand: readonly [U, V];
36
36
  }, Readonly<{
37
37
  readonly type: "string";
38
38
  } & {
39
- optional: true;
39
+ brand: readonly [U, V];
40
40
  }>>;
41
- description: (description: string) => ExtWith_Option<{
41
+ minLength: (minLength: number) => ExtWith_Option<{
42
42
  readonly type: "string";
43
43
  }, {
44
- description: string;
44
+ minLength: number;
45
45
  }, Readonly<{
46
46
  readonly type: "string";
47
47
  } & {
48
- description: string;
48
+ minLength: number;
49
49
  }>>;
50
- brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
50
+ maxLength: (maxLength: number) => ExtWith_Option<{
51
51
  readonly type: "string";
52
52
  }, {
53
- brand: readonly [U, V];
53
+ maxLength: number;
54
54
  }, Readonly<{
55
55
  readonly type: "string";
56
56
  } & {
57
- brand: readonly [U, V];
57
+ maxLength: number;
58
58
  }>>;
59
- minLength: (minLength: number) => ExtWith_Option<{
59
+ optional: () => ExtWith_Option<{
60
60
  readonly type: "string";
61
61
  }, {
62
- minLength: number;
62
+ optional: true;
63
63
  }, Readonly<{
64
64
  readonly type: "string";
65
65
  } & {
66
- minLength: number;
66
+ optional: true;
67
67
  }>>;
68
- maxLength: (maxLength: number) => ExtWith_Option<{
68
+ description: (description: string) => ExtWith_Option<{
69
69
  readonly type: "string";
70
70
  }, {
71
- maxLength: number;
71
+ description: string;
72
72
  }, Readonly<{
73
73
  readonly type: "string";
74
74
  } & {
75
- maxLength: number;
75
+ description: string;
76
76
  }>>;
77
77
  __schema: {
78
78
  readonly type: "string";
@@ -1,11 +1,10 @@
1
- import type { BS_Schema, Con_BS_Schema_SubjT } from './base-short-schema-types'
2
1
  import type {
3
2
  BD_Schema,
4
3
  Con_BD_Schema_SubjT_P,
5
4
  Con_BD_Schema_SubjT_V,
6
5
  } from './base-detailed-schema-types'
7
6
 
8
- export type BaseSchema = BS_Schema | BD_Schema
7
+ export type BaseSchema = BD_Schema
9
8
  export type CompoundSchema = ObjectSchema | ArraySchema
10
9
  export type Schema = BaseSchema | CompoundSchema
11
10
 
@@ -46,17 +45,13 @@ export type ObjectSchema<T extends Schema = NestedSchema> =
46
45
 
47
46
  /* Construct BaseSchema subject type */
48
47
 
49
- export type Con_BaseSchema_SubjT_P<T extends BaseSchema> = T extends BS_Schema
50
- ? Con_BS_Schema_SubjT<T>
51
- : T extends BD_Schema
52
- ? Con_BD_Schema_SubjT_P<T>
53
- : 'NEVER:Con_BaseSchema_SubjT_P'
54
-
55
- export type Con_BaseSchema_SubjT_V<T extends BaseSchema> = T extends BS_Schema
56
- ? Con_BS_Schema_SubjT<T>
57
- : T extends BD_Schema
58
- ? Con_BD_Schema_SubjT_V<T>
59
- : never
48
+ // TODO: get rid of this intermediate type when the following issue is ready:
49
+ // "Remove support of default value on the schema level"
50
+ // https://github.com/incerta/schematox/issues/14
51
+ export type Con_BaseSchema_SubjT_P<T extends BaseSchema> =
52
+ Con_BD_Schema_SubjT_P<T>
53
+ export type Con_BaseSchema_SubjT_V<T extends BaseSchema> =
54
+ Con_BD_Schema_SubjT_V<T>
60
55
 
61
56
  /* Compound schema utility types */
62
57
 
@@ -104,7 +99,7 @@ export type Con_ObjectSchema_SubjT_P<T extends ObjectSchema> =
104
99
  of: infer U
105
100
  }
106
101
  ? {
107
- [k in keyof U]: U[k] extends BaseSchema
102
+ -readonly [k in keyof U]: U[k] extends BaseSchema
108
103
  ? Con_BaseSchema_SubjT_P<U[k]>
109
104
  : U[k] extends ObjectSchema
110
105
  ? Con_ObjectSchema_SubjT_P<U[k]>
@@ -122,7 +117,7 @@ export type Con_ObjectSchema_SubjT_V<T extends ObjectSchema> =
122
117
  of: infer U
123
118
  }
124
119
  ? {
125
- [k in keyof U]: U[k] extends BaseSchema
120
+ -readonly [k in keyof U]: U[k] extends BaseSchema
126
121
  ? Con_BaseSchema_SubjT_V<U[k]>
127
122
  : U[k] extends ObjectSchema
128
123
  ? Con_ObjectSchema_SubjT_V<U[k]>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "schematox",
3
- "version": "0.0.5",
3
+ "version": "0.1.0",
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",
@@ -1,72 +0,0 @@
1
- import type { BD_Buffer } from '../types/base-detailed-schema-types';
2
- type ExtWith_Option<T extends BD_Buffer, U, V extends BD_Buffer = Readonly<T & U>> = {
3
- __schema: V;
4
- } & BufferOptions<V>;
5
- type BufferOptions<T extends BD_Buffer> = Omit<{
6
- optional: () => ExtWith_Option<T, {
7
- optional: true;
8
- }>;
9
- brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<T, {
10
- brand: Readonly<[U, V]>;
11
- }>;
12
- minLength: (minLength: number) => ExtWith_Option<T, {
13
- minLength: number;
14
- }>;
15
- maxLength: (maxLength: number) => ExtWith_Option<T, {
16
- maxLength: number;
17
- }>;
18
- description: (description: string) => ExtWith_Option<T, {
19
- description: string;
20
- }>;
21
- }, keyof T>;
22
- export declare function buffer(): {
23
- optional: () => ExtWith_Option<{
24
- readonly type: "buffer";
25
- }, {
26
- optional: true;
27
- }, Readonly<{
28
- readonly type: "buffer";
29
- } & {
30
- optional: true;
31
- }>>;
32
- description: (description: string) => ExtWith_Option<{
33
- readonly type: "buffer";
34
- }, {
35
- description: string;
36
- }, Readonly<{
37
- readonly type: "buffer";
38
- } & {
39
- description: string;
40
- }>>;
41
- brand: <U extends string, V extends string>(key: U, value: V) => ExtWith_Option<{
42
- readonly type: "buffer";
43
- }, {
44
- brand: readonly [U, V];
45
- }, Readonly<{
46
- readonly type: "buffer";
47
- } & {
48
- brand: readonly [U, V];
49
- }>>;
50
- minLength: (minLength: number) => ExtWith_Option<{
51
- readonly type: "buffer";
52
- }, {
53
- minLength: number;
54
- }, Readonly<{
55
- readonly type: "buffer";
56
- } & {
57
- minLength: number;
58
- }>>;
59
- maxLength: (maxLength: number) => ExtWith_Option<{
60
- readonly type: "buffer";
61
- }, {
62
- maxLength: number;
63
- }, Readonly<{
64
- readonly type: "buffer";
65
- } & {
66
- maxLength: number;
67
- }>>;
68
- __schema: {
69
- readonly type: "buffer";
70
- };
71
- };
72
- export {};
@@ -1,61 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.buffer = void 0;
15
- var error_1 = require("../error");
16
- function bufferOptions(schema) {
17
- var schemaKeys = Object.keys(schema);
18
- var except = new Set(schemaKeys);
19
- return {
20
- brand: function (key, value) {
21
- if (except.has('brand')) {
22
- throw Error(error_1.PROGRAMMATICALLY_DEFINED_ERROR_MSG.brandDefined);
23
- }
24
- var updatedSchema = __assign(__assign({}, schema), { brand: [key, value] });
25
- return __assign({ __schema: updatedSchema }, bufferOptions(updatedSchema));
26
- },
27
- optional: function () {
28
- if (except.has('optional')) {
29
- throw Error(error_1.PROGRAMMATICALLY_DEFINED_ERROR_MSG.optionalDefined);
30
- }
31
- var updatedSchema = __assign(__assign({}, schema), { optional: true });
32
- return __assign({ __schema: updatedSchema }, bufferOptions(updatedSchema));
33
- },
34
- minLength: function (minLength) {
35
- if (except.has('minLength')) {
36
- throw Error(error_1.PROGRAMMATICALLY_DEFINED_ERROR_MSG.minLengthDefined);
37
- }
38
- var updatedSchema = __assign(__assign({}, schema), { minLength: minLength });
39
- return __assign({ __schema: updatedSchema }, bufferOptions(updatedSchema));
40
- },
41
- maxLength: function (maxLength) {
42
- if (except.has('maxLength')) {
43
- throw Error(error_1.PROGRAMMATICALLY_DEFINED_ERROR_MSG.maxLengthDefined);
44
- }
45
- var updatedSchema = __assign(__assign({}, schema), { maxLength: maxLength });
46
- return __assign({ __schema: updatedSchema }, bufferOptions(updatedSchema));
47
- },
48
- description: function (description) {
49
- if (except.has('description')) {
50
- throw Error(error_1.PROGRAMMATICALLY_DEFINED_ERROR_MSG.descriptionDefined);
51
- }
52
- var updatedSchema = __assign(__assign({}, schema), { description: description });
53
- return __assign({ __schema: updatedSchema }, bufferOptions(updatedSchema));
54
- },
55
- };
56
- }
57
- function buffer() {
58
- var schema = { type: 'buffer' };
59
- return __assign({ __schema: schema }, bufferOptions(schema));
60
- }
61
- exports.buffer = buffer;
@@ -1,38 +0,0 @@
1
- export type BS_String_Req = 'string'
2
- export type BS_String_Opt = 'string?'
3
-
4
- export type BS_Number_Req = 'number'
5
- export type BS_Number_Opt = 'number?'
6
-
7
- export type BS_Boolean_Req = 'boolean'
8
- export type BS_Boolean_Opt = 'boolean?'
9
-
10
- export type BS_Schema_Req = BS_String_Req | BS_Number_Req | BS_Boolean_Req
11
-
12
- export type BS_Schema_Opt = BS_String_Opt | BS_Number_Opt | BS_Boolean_Opt
13
-
14
- export type BS_Schema = BS_Schema_Req | BS_Schema_Opt
15
-
16
- export type Con_BS_Schema_Req_SubjT<T extends BS_Schema_Req> =
17
- T extends BS_String_Req
18
- ? string
19
- : T extends BS_Number_Req
20
- ? number
21
- : T extends BS_Boolean_Req
22
- ? boolean
23
- : never
24
-
25
- export type Con_BS_Schema_Opt_SubjT<T extends BS_Schema_Opt> =
26
- T extends BS_String_Opt
27
- ? string | undefined
28
- : T extends BS_Number_Opt
29
- ? number | undefined
30
- : T extends BS_Boolean_Opt
31
- ? boolean | undefined
32
- : never
33
-
34
- export type Con_BS_Schema_SubjT<T extends BS_Schema> = T extends BS_Schema_Req
35
- ? Con_BS_Schema_Req_SubjT<T>
36
- : T extends BS_Schema_Opt
37
- ? Con_BS_Schema_Opt_SubjT<T>
38
- : never