@cleverbrush/schema 1.0.0-beta.0 → 1.0.0-beta.2

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 (49) hide show
  1. package/package.json +2 -2
  2. package/src/builders/UnionSchemaBuilder.ts +1 -6
  3. package/src/index.ts +1 -13
  4. package/src/schema.ts +9 -104
  5. package/src/schemaRegistry.builders.test.ts +35 -80
  6. package/src/schemaRegistry.test.ts +2 -1916
  7. package/src/schemaRegistry.ts +55 -126
  8. package/dist/builders/AliasSchemaBuilder.d.ts +0 -14
  9. package/dist/builders/AliasSchemaBuilder.js +0 -91
  10. package/dist/builders/ArraySchemaBuilder.d.ts +0 -15
  11. package/dist/builders/ArraySchemaBuilder.js +0 -141
  12. package/dist/builders/BooleanSchemaBuilder.d.ts +0 -31
  13. package/dist/builders/BooleanSchemaBuilder.js +0 -90
  14. package/dist/builders/FunctionSchemaBuilder.d.ts +0 -25
  15. package/dist/builders/FunctionSchemaBuilder.js +0 -66
  16. package/dist/builders/NumberSchemaBuilder.d.ts +0 -61
  17. package/dist/builders/NumberSchemaBuilder.js +0 -206
  18. package/dist/builders/ObjectSchemaBuilder.d.ts +0 -83
  19. package/dist/builders/ObjectSchemaBuilder.js +0 -344
  20. package/dist/builders/SchemaBuilder.d.ts +0 -36
  21. package/dist/builders/SchemaBuilder.js +0 -88
  22. package/dist/builders/StringSchemaBuilder.d.ts +0 -14
  23. package/dist/builders/StringSchemaBuilder.js +0 -140
  24. package/dist/builders/UnionSchemaBuilder.d.ts +0 -10
  25. package/dist/builders/UnionSchemaBuilder.js +0 -100
  26. package/dist/defaultSchemas.d.ts +0 -4
  27. package/dist/defaultSchemas.js +0 -45
  28. package/dist/index.d.ts +0 -42
  29. package/dist/index.js +0 -35
  30. package/dist/schema.d.ts +0 -242
  31. package/dist/schema.js +0 -2
  32. package/dist/schemaRegistry.d.ts +0 -54
  33. package/dist/schemaRegistry.js +0 -301
  34. package/dist/validators/validateArray.d.ts +0 -3
  35. package/dist/validators/validateArray.js +0 -60
  36. package/dist/validators/validateBoolean.d.ts +0 -2
  37. package/dist/validators/validateBoolean.js +0 -30
  38. package/dist/validators/validateFunction.d.ts +0 -2
  39. package/dist/validators/validateFunction.js +0 -21
  40. package/dist/validators/validateNumber.d.ts +0 -2
  41. package/dist/validators/validateNumber.js +0 -69
  42. package/dist/validators/validateObject.d.ts +0 -3
  43. package/dist/validators/validateObject.js +0 -95
  44. package/dist/validators/validateString.d.ts +0 -2
  45. package/dist/validators/validateString.js +0 -50
  46. package/dist/validators/validateUnion.d.ts +0 -3
  47. package/dist/validators/validateUnion.js +0 -30
  48. package/src/builders/AliasSchemaBuilder.test.ts +0 -124
  49. package/src/builders/AliasSchemaBuilder.ts +0 -250
@@ -18,7 +18,6 @@ import {
18
18
  StringSchema,
19
19
  ValidationResult,
20
20
  ValidationResultRaw,
21
- InferType,
22
21
  ExpandSchemaBuilder,
23
22
  FunctionSchema
24
23
  } from './schema.js';
@@ -29,11 +28,6 @@ import { object } from './builders/ObjectSchemaBuilder.js';
29
28
  import { string } from './builders/StringSchemaBuilder.js';
30
29
  import { boolean } from './builders/BooleanSchemaBuilder.js';
31
30
  import { array } from './builders/ArraySchemaBuilder.js';
32
- import {
33
- alias,
34
- externalAlias,
35
- IAliasSchemaBuilder
36
- } from './builders/AliasSchemaBuilder.js';
37
31
  import { func } from './builders/FunctionSchemaBuilder.js';
38
32
  import { defaultSchemas } from './defaultSchemas.js';
39
33
  import { ISchemaBuilder, SchemaBuilder } from './builders/SchemaBuilder.js';
@@ -86,13 +80,41 @@ export default class SchemaRegistry<T extends Record<string, Schema> = {}>
86
80
  return this;
87
81
  }
88
82
 
83
+ private _aliasBuilder = <TSchemaName extends keyof T>(
84
+ schemaName: TSchemaName
85
+ ): ExpandSchemaBuilder<T[TSchemaName]> => {
86
+ if (typeof schemaName !== 'string' || !schemaName)
87
+ throw new Error('schemaName must be non-empty string');
88
+ const aliasSchema = this._schemasMap.get(schemaName.toString());
89
+ if (!aliasSchema)
90
+ throw new Error(`Schema name ${schemaName} is not defined.`);
91
+ return aliasSchema;
92
+ };
93
+
94
+ private _externalAlias = <
95
+ TExternalSchemaName extends keyof TExternalAliases,
96
+ TExternalAliases extends Record<string, any>,
97
+ TExternalSchemaRegistry extends ISchemaRegistry<TExternalAliases>
98
+ >(
99
+ registry: TExternalSchemaRegistry,
100
+ schemaName: TExternalSchemaName
101
+ ): ExpandSchemaBuilder<TExternalAliases[TExternalSchemaName]> => {
102
+ if (typeof schemaName !== 'string' || !schemaName)
103
+ throw new Error('schemaName must be non-empty string');
104
+ // TODO: accessing private property here...
105
+ const externalSchema = (registry as any)._schemasMap.get(schemaName);
106
+ if (!externalSchema)
107
+ throw new Error(`Schema name ${schemaName} is not defined.`);
108
+ return externalSchema;
109
+ };
110
+
89
111
  public addSchemaFrom<
90
112
  TOldName extends keyof T,
91
113
  TName extends string,
92
114
  TParam extends
93
115
  | Record<string, any>
94
116
  | ((params: {
95
- schema: ExpandSchemaBuilder<T[TOldName]>;
117
+ base: ExpandSchemaBuilder<T[TOldName]>;
96
118
  boolean: typeof boolean;
97
119
  func: typeof func;
98
120
  array: typeof array;
@@ -100,35 +122,22 @@ export default class SchemaRegistry<T extends Record<string, Schema> = {}>
100
122
  number: typeof number;
101
123
  union: typeof union;
102
124
  object: typeof object;
103
- alias: <
104
- TSchemaName extends keyof T,
105
- TCompiledType = InferType<
106
- ExpandSchemaBuilder<T[TSchemaName]>
107
- >
108
- >(
125
+ alias: <TSchemaName extends keyof T>(
109
126
  schemaName: TSchemaName
110
- ) => IAliasSchemaBuilder<
111
- TSchemaName,
112
- true,
113
- false,
114
- {
115
- [k in keyof T]: TSchemaName extends k ? T[k] : never;
116
- },
117
- TCompiledType
118
- >;
127
+ ) => ExpandSchemaBuilder<T[TSchemaName]>;
119
128
  }) => TSchema),
120
129
  TSchema extends Schema | ISchemaBuilder
121
130
  >(
122
131
  oldName: TOldName,
123
132
  name: TName,
124
133
  param: TParam
125
- ): SchemaRegistry<
126
- T & {
127
- [key in TName]: TParam extends (...args: any[]) => any
128
- ? ReduceSchemaBuilder<ReturnType<TParam>>
129
- : TParam;
130
- }
131
- > {
134
+ ): SchemaRegistry<{
135
+ [key in keyof T | TName]: key extends keyof T
136
+ ? T[key]
137
+ : TParam extends (...args: any[]) => any
138
+ ? ReduceSchemaBuilder<ReturnType<TParam>>
139
+ : TParam;
140
+ }> {
132
141
  if (!this._schemasMap.has(oldName.toString())) {
133
142
  throw new Error(`unknown schema name ${oldName.toString()}`);
134
143
  }
@@ -138,8 +147,8 @@ export default class SchemaRegistry<T extends Record<string, Schema> = {}>
138
147
  const schema =
139
148
  typeof param === 'function'
140
149
  ? param({
141
- schema: oldSchema,
142
- alias,
150
+ base: oldSchema,
151
+ alias: this._aliasBuilder,
143
152
  array,
144
153
  boolean,
145
154
  func,
@@ -180,54 +189,28 @@ export default class SchemaRegistry<T extends Record<string, Schema> = {}>
180
189
  union: typeof union;
181
190
  object: typeof object;
182
191
  func: typeof func;
183
- alias: <
184
- TSchemaName extends keyof T,
185
- TCompiledType = InferType<
186
- ExpandSchemaBuilder<T[TSchemaName]>
187
- >
188
- >(
192
+ alias: <TSchemaName extends keyof T>(
189
193
  schemaName: TSchemaName
190
- ) => IAliasSchemaBuilder<
191
- TSchemaName,
192
- true,
193
- false,
194
- {
195
- [k in keyof T]: TSchemaName extends k ? T[k] : never;
196
- },
197
- TCompiledType
198
- >;
194
+ ) => ExpandSchemaBuilder<T[TSchemaName]>;
199
195
  external: <
200
- TRegistryKeys extends Record<string, any>,
201
- TSchemaName extends keyof TRegistryKeys,
202
- TCompiledType = InferType<
203
- ExpandSchemaBuilder<TRegistryKeys[TSchemaName]>
204
- >
196
+ TRegistryKeys,
197
+ TSchemaName extends keyof TRegistryKeys
205
198
  >(
206
- registry: ISchemaRegistry<TRegistryKeys>,
199
+ registry: SchemaRegistry<TRegistryKeys>,
207
200
  schemaName: TSchemaName
208
- ) => IAliasSchemaBuilder<
209
- TSchemaName,
210
- true,
211
- false,
212
- {
213
- [k in keyof TRegistryKeys]: TSchemaName extends k
214
- ? TRegistryKeys[k]
215
- : never;
216
- },
217
- TCompiledType
218
- >;
201
+ ) => ExpandSchemaBuilder<TRegistryKeys[TSchemaName]>;
219
202
  }) => TSchema),
220
203
  TSchema extends Schema | ISchemaBuilder
221
204
  >(
222
205
  name: TName,
223
206
  param: TParam
224
- ): SchemaRegistry<
225
- T & {
226
- [key in TName]: TParam extends (...args: any[]) => any
227
- ? ReduceSchemaBuilder<ReturnType<TParam>>
228
- : TParam;
229
- }
230
- > {
207
+ ): SchemaRegistry<{
208
+ [key in keyof T | TName]: key extends keyof T
209
+ ? T[key]
210
+ : TParam extends (...args: any[]) => any
211
+ ? ReduceSchemaBuilder<ReturnType<TParam>>
212
+ : TParam;
213
+ }> {
231
214
  if (typeof name !== 'string' || !name)
232
215
  throw new Error('Name is required');
233
216
 
@@ -242,8 +225,8 @@ export default class SchemaRegistry<T extends Record<string, Schema> = {}>
242
225
  const schema =
243
226
  typeof param === 'function'
244
227
  ? param({
245
- alias,
246
- external: externalAlias,
228
+ alias: this._aliasBuilder,
229
+ external: this._externalAlias,
247
230
  array,
248
231
  boolean,
249
232
  number,
@@ -454,60 +437,6 @@ export default class SchemaRegistry<T extends Record<string, Schema> = {}>
454
437
  obj,
455
438
  schema
456
439
  );
457
- } else if (spec.type === 'alias') {
458
- if (spec.isRequired === false && typeof obj === 'undefined') {
459
- return {
460
- valid: true
461
- };
462
- }
463
- if (spec.isNullable === true && obj === null) {
464
- return {
465
- valid: true
466
- };
467
- }
468
-
469
- const registry = (spec.externalRegistry ||
470
- this) as ISchemaRegistry<any>;
471
- const alias = (registry as any)._schemasMap.get(
472
- spec.schemaName
473
- );
474
- if (typeof alias === 'undefined') {
475
- throw new Error(
476
- `Unknown schema alias - ${spec.schemaName}`
477
- );
478
- }
479
- if (Array.isArray(alias)) {
480
- if (
481
- (!spec.isRequired && typeof obj === 'undefined') ||
482
- (spec.isNullable && obj === null)
483
- ) {
484
- return {
485
- valid: true
486
- };
487
- }
488
- return await (registry as any).validate(alias as any, obj);
489
- }
490
- if (typeof alias !== 'object')
491
- throw new Error(
492
- 'it is only possible to use a full schema schema definition as alias'
493
- );
494
-
495
- let schemaToValidate = alias;
496
-
497
- if (alias instanceof SchemaBuilder) {
498
- if (typeof schema.isRequired === 'boolean') {
499
- schemaToValidate = (schemaToValidate as any)[
500
- schema.isRequired ? 'required' : 'optional'
501
- ]();
502
- }
503
- if (typeof schema.isNullable === 'boolean') {
504
- schemaToValidate = (schemaToValidate as any)[
505
- schema.isNullable ? 'nullable' : 'notNullable'
506
- ]();
507
- }
508
- }
509
-
510
- return await (registry as any).validate(schemaToValidate, obj);
511
440
  } else if (spec.type === 'union') {
512
441
  return await validateUnion(obj, spec, this as any);
513
442
  } else if (spec.type === 'object') {
@@ -1,14 +0,0 @@
1
- import { ISchemaBuilder } from './SchemaBuilder.js';
2
- import { InferType } from '../schema.js';
3
- import { ISchemaRegistry } from '../index.js';
4
- export interface IAliasSchemaBuilder<TSchemaName extends keyof TAliases, TRequired extends boolean = true, TNullable extends boolean = false, TAliases extends Record<string, any> = never, TAliasesCompiledType = any, TExternalRegistryKeys extends Record<string, any> = {}> extends ISchemaBuilder<TRequired, TNullable> {
5
- readonly type: 'alias';
6
- schemaName: TSchemaName;
7
- externalRegistry: ISchemaRegistry<TExternalRegistryKeys>;
8
- optional(): IAliasSchemaBuilder<TSchemaName, false, TNullable, TAliases, TAliasesCompiledType, TExternalRegistryKeys>;
9
- required(): IAliasSchemaBuilder<TSchemaName, true, TNullable, TAliases, TAliasesCompiledType, TExternalRegistryKeys>;
10
- nullable(): IAliasSchemaBuilder<TSchemaName, TRequired, true, TAliases, TAliasesCompiledType, TExternalRegistryKeys>;
11
- notNullable(): IAliasSchemaBuilder<TSchemaName, TRequired, false, TAliases, TAliasesCompiledType, TExternalRegistryKeys>;
12
- }
13
- export declare const alias: <TSchemaName extends keyof TAliases, TAliases extends Record<string, any>, TAliasesCompiledType = InferType<TAliases[TSchemaName]>>(schemaName: TSchemaName) => IAliasSchemaBuilder<TSchemaName, true, false, TAliases, TAliasesCompiledType, {}>;
14
- export declare const externalAlias: <TSchemaName extends keyof TAliases, TAliases extends Record<string, any>, TExternalSchemaRegistry extends ISchemaRegistry<TAliases>, TAliasesCompiledType = InferType<TAliases[TSchemaName]>>(registry: TExternalSchemaRegistry, schemaName: TSchemaName) => IAliasSchemaBuilder<TSchemaName, true, false, TAliases, TAliasesCompiledType, {}>;
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.externalAlias = exports.alias = void 0;
4
- const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
5
- const defaultSchemas_js_1 = require("../defaultSchemas.js");
6
- class AliasSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
7
- clone() {
8
- return AliasSchemaBuilder.create(this._schema);
9
- }
10
- get _schema() {
11
- return Object.assign({
12
- type: this.type,
13
- schemaName: this.schemaName,
14
- externalRegistry: this.externalRegistry
15
- }, this.getCommonSchema());
16
- }
17
- static create(obj) {
18
- return new AliasSchemaBuilder(obj);
19
- }
20
- schemaName;
21
- externalRegistry;
22
- constructor(obj) {
23
- super(obj);
24
- if (typeof obj === 'object' && obj) {
25
- if (typeof obj.schemaName === 'string') {
26
- this.schemaName = obj.schemaName;
27
- }
28
- if (typeof obj.externalRegistry === 'object' &&
29
- obj.externalRegistry) {
30
- this.externalRegistry = obj.externalRegistry;
31
- }
32
- }
33
- else {
34
- const defaultSchema = defaultSchemas_js_1.defaultSchemas['alias'];
35
- this.isRequired = defaultSchema.isRequired;
36
- this.isNullable = defaultSchema.isNullable;
37
- }
38
- }
39
- get type() {
40
- return 'alias';
41
- }
42
- optional() {
43
- if (this.isRequired === false) {
44
- return this;
45
- }
46
- return AliasSchemaBuilder.create({
47
- ...this._schema,
48
- isRequired: false
49
- });
50
- }
51
- required() {
52
- if (this.isRequired === true) {
53
- return this;
54
- }
55
- return AliasSchemaBuilder.create({
56
- ...this._schema,
57
- isRequired: true
58
- });
59
- }
60
- nullable() {
61
- if (this._isNullable === true) {
62
- return this;
63
- }
64
- return AliasSchemaBuilder.create({
65
- ...this._schema,
66
- isNullable: true
67
- });
68
- }
69
- notNullable() {
70
- if (this.isNullable === false) {
71
- return this;
72
- }
73
- return AliasSchemaBuilder.create({
74
- ...this._schema,
75
- isNullable: false
76
- });
77
- }
78
- }
79
- const alias = (schemaName) => AliasSchemaBuilder.create({
80
- schemaName,
81
- isRequired: true,
82
- isNullable: false
83
- });
84
- exports.alias = alias;
85
- const externalAlias = (registry, schemaName) => AliasSchemaBuilder.create({
86
- schemaName,
87
- isRequired: true,
88
- isNullable: false,
89
- externalRegistry: registry
90
- });
91
- exports.externalAlias = externalAlias;
@@ -1,15 +0,0 @@
1
- import { ISchemaBuilder } from './SchemaBuilder.js';
2
- import { Schema } from '../schema.js';
3
- export interface IArraySchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TOfType extends Schema | undefined = undefined, TMaxLength extends number | undefined = undefined, TMinLength extends number | undefined = undefined> extends ISchemaBuilder<TRequired, TNullable> {
4
- optional(): IArraySchemaBuilder<false, TNullable, TOfType, TMaxLength, TMinLength>;
5
- required(): IArraySchemaBuilder<true, TNullable, TOfType, TMaxLength, TMinLength>;
6
- ofType<T extends Schema>(schema: T): IArraySchemaBuilder<TRequired, TNullable, T, TMaxLength, TMinLength>;
7
- nullable(): IArraySchemaBuilder<TRequired, true, TOfType, TMaxLength, TMinLength>;
8
- notNullable(): IArraySchemaBuilder<TRequired, false, TOfType, TMaxLength, TMinLength>;
9
- clearOfType(): IArraySchemaBuilder<TRequired, TNullable, undefined, TMaxLength, TMinLength>;
10
- maxLength<T extends number>(length: T): IArraySchemaBuilder<TRequired, TNullable, TOfType, T, TMinLength>;
11
- clearMaxLength(): IArraySchemaBuilder<TRequired, TNullable, TOfType, undefined, TMinLength>;
12
- minLength<T extends number>(length: T): IArraySchemaBuilder<TRequired, TNullable, TOfType, TMaxLength, T>;
13
- clearMinLength(): IArraySchemaBuilder<TRequired, TNullable, TOfType, TMaxLength, TMinLength>;
14
- }
15
- export declare const array: () => IArraySchemaBuilder<true, false, undefined, undefined, undefined>;
@@ -1,141 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.array = void 0;
4
- const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
5
- const defaultSchemas_js_1 = require("../defaultSchemas.js");
6
- class ArraySchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
7
- clone() {
8
- return ArraySchemaBuilder.create(this._schema);
9
- }
10
- get _schema() {
11
- return Object.assign({
12
- type: this.type
13
- }, this.getCommonSchema(), typeof this._ofType !== 'undefined'
14
- ? {
15
- ofType: this._ofType
16
- }
17
- : {}, typeof this._minLength !== 'undefined'
18
- ? { minLength: this._minLength }
19
- : {}, typeof this._maxLength !== 'undefined'
20
- ? { maxLength: this._maxLength }
21
- : {});
22
- }
23
- static create(obj) {
24
- return new ArraySchemaBuilder(obj);
25
- }
26
- constructor(obj) {
27
- super(obj);
28
- if (typeof obj === 'object' && obj) {
29
- if (typeof obj.ofType !== 'undefined') {
30
- this._ofType =
31
- obj.ofType instanceof SchemaBuilder_js_1.SchemaBuilder
32
- ? obj.ofType.clone()
33
- : { ...obj.ofType };
34
- }
35
- if (typeof obj.maxLength !== 'undefined') {
36
- this._maxLength = obj.maxLength;
37
- }
38
- if (typeof obj.minLength !== 'undefined') {
39
- this._minLength = obj.minLength;
40
- }
41
- }
42
- else {
43
- const defaultSchema = defaultSchemas_js_1.defaultSchemas['array'];
44
- this.isRequired = defaultSchema.isRequired;
45
- this.isNullable = defaultSchema.isNullable;
46
- }
47
- }
48
- type = 'array';
49
- _ofType;
50
- _maxLength;
51
- _minLength;
52
- optional() {
53
- if (this.isRequired === false) {
54
- return this;
55
- }
56
- return ArraySchemaBuilder.create({
57
- ...this._schema,
58
- isRequired: false
59
- });
60
- }
61
- required() {
62
- if (this.isRequired === true) {
63
- return this;
64
- }
65
- return ArraySchemaBuilder.create({
66
- ...this._schema,
67
- isRequired: true
68
- });
69
- }
70
- nullable() {
71
- if (this._isNullable === true) {
72
- return this;
73
- }
74
- return ArraySchemaBuilder.create({
75
- ...this._schema,
76
- isNullable: true
77
- });
78
- }
79
- notNullable() {
80
- if (this.isNullable === false) {
81
- return this;
82
- }
83
- return ArraySchemaBuilder.create({
84
- ...this._schema,
85
- isNullable: false
86
- });
87
- }
88
- ofType(schema) {
89
- return ArraySchemaBuilder.create({
90
- ...this._schema,
91
- ofType: schema
92
- });
93
- }
94
- clearOfType() {
95
- if (typeof this._ofType === 'undefined') {
96
- return this;
97
- }
98
- return ArraySchemaBuilder.create({
99
- ...this._schema,
100
- ofType: undefined
101
- });
102
- }
103
- maxLength(length) {
104
- if (this._maxLength === length) {
105
- return this;
106
- }
107
- return ArraySchemaBuilder.create({
108
- ...this._schema,
109
- maxLength: length
110
- });
111
- }
112
- clearMaxLength() {
113
- if (typeof this._maxLength === 'undefined') {
114
- return this;
115
- }
116
- return ArraySchemaBuilder.create({
117
- ...this._schema,
118
- maxLength: undefined
119
- });
120
- }
121
- minLength(length) {
122
- if (this._minLength === length) {
123
- return this;
124
- }
125
- return ArraySchemaBuilder.create({
126
- ...this._schema,
127
- minLength: length
128
- });
129
- }
130
- clearMinLength() {
131
- if (typeof this._minLength === 'undefined') {
132
- return this;
133
- }
134
- return ArraySchemaBuilder.create({
135
- ...this._schema,
136
- minLength: undefined
137
- });
138
- }
139
- }
140
- const array = () => ArraySchemaBuilder.create();
141
- exports.array = array;
@@ -1,31 +0,0 @@
1
- import { Schema, Validator } from '../schema.js';
2
- import { ISchemaBuilder, SchemaBuilder } from './SchemaBuilder.js';
3
- export interface IBooleanSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TEqualsTo extends boolean | undefined = undefined> extends ISchemaBuilder<TRequired, TNullable> {
4
- equals<T extends boolean>(val: T): IBooleanSchemaBuilder<TRequired, TNullable, T>;
5
- clearEqualsTo(): IBooleanSchemaBuilder<TRequired, TNullable, undefined>;
6
- optional(): IBooleanSchemaBuilder<false, TNullable, TEqualsTo>;
7
- required(): IBooleanSchemaBuilder<true, TNullable, TEqualsTo>;
8
- nullable(): IBooleanSchemaBuilder<TRequired, true, TEqualsTo>;
9
- notNullable(): IBooleanSchemaBuilder<TRequired, false, TEqualsTo>;
10
- }
11
- export declare class BooleanSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TEqualsTo extends boolean | undefined = undefined> extends SchemaBuilder<TRequired, TNullable> implements IBooleanSchemaBuilder<TRequired, TNullable, TEqualsTo> {
12
- protected readonly type = "boolean";
13
- get _schema(): Schema;
14
- clone(): this;
15
- static create<TRequired extends boolean = true, TNullable extends boolean = false, TEqualsTo extends boolean | undefined = undefined>(obj?: {
16
- isRequired: TRequired;
17
- isNullable: TNullable;
18
- equals?: TEqualsTo;
19
- validators?: Validator[];
20
- preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
21
- }): IBooleanSchemaBuilder<TRequired, TNullable, TEqualsTo>;
22
- private constructor();
23
- protected _equals?: TEqualsTo;
24
- equals<T extends boolean>(val: T): IBooleanSchemaBuilder<TRequired, TNullable, T>;
25
- clearEqualsTo(): IBooleanSchemaBuilder<TRequired, TNullable, undefined>;
26
- optional(): IBooleanSchemaBuilder<false, TNullable, TEqualsTo>;
27
- required(): IBooleanSchemaBuilder<true, TNullable, TEqualsTo>;
28
- nullable(): IBooleanSchemaBuilder<TRequired, true, TEqualsTo>;
29
- notNullable(): IBooleanSchemaBuilder<TRequired, false, TEqualsTo>;
30
- }
31
- export declare const boolean: () => IBooleanSchemaBuilder;
@@ -1,90 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.boolean = exports.BooleanSchemaBuilder = void 0;
4
- const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
5
- const defaultSchemas_js_1 = require("../defaultSchemas.js");
6
- class BooleanSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
7
- type = 'boolean';
8
- get _schema() {
9
- return Object.assign({
10
- type: this.type
11
- }, this.getCommonSchema(), typeof this._equals !== 'undefined' ? { equals: this._equals } : {});
12
- }
13
- clone() {
14
- return BooleanSchemaBuilder.create(this._schema);
15
- }
16
- static create(obj) {
17
- return new BooleanSchemaBuilder(obj);
18
- }
19
- constructor(obj) {
20
- super(obj);
21
- if (typeof obj === 'object' && obj) {
22
- if (typeof obj.equals !== 'undefined') {
23
- this._equals = obj.equals;
24
- }
25
- }
26
- else {
27
- const defaultSchema = defaultSchemas_js_1.defaultSchemas['boolean'];
28
- this.isRequired = defaultSchema.isRequired;
29
- this.isNullable = defaultSchema.isNullable;
30
- }
31
- }
32
- _equals;
33
- equals(val) {
34
- if (this._equals === val) {
35
- return this;
36
- }
37
- return BooleanSchemaBuilder.create({
38
- ...this._schema,
39
- equals: val
40
- });
41
- }
42
- clearEqualsTo() {
43
- if (typeof this._equals === 'undefined') {
44
- return this;
45
- }
46
- return BooleanSchemaBuilder.create({
47
- ...this._schema,
48
- equals: undefined
49
- });
50
- }
51
- optional() {
52
- if (this.isRequired === false) {
53
- return this;
54
- }
55
- return BooleanSchemaBuilder.create({
56
- ...this._schema,
57
- isRequired: false
58
- });
59
- }
60
- required() {
61
- if (this.isRequired === true) {
62
- return this;
63
- }
64
- return BooleanSchemaBuilder.create({
65
- ...this._schema,
66
- isRequired: true
67
- });
68
- }
69
- nullable() {
70
- if (this.isNullable === true) {
71
- return this;
72
- }
73
- return BooleanSchemaBuilder.create({
74
- ...this._schema,
75
- isNullable: true
76
- });
77
- }
78
- notNullable() {
79
- if (this.isNullable === false) {
80
- return this;
81
- }
82
- return BooleanSchemaBuilder.create({
83
- ...this._schema,
84
- isNullable: false
85
- });
86
- }
87
- }
88
- exports.BooleanSchemaBuilder = BooleanSchemaBuilder;
89
- const boolean = () => BooleanSchemaBuilder.create();
90
- exports.boolean = boolean;
@@ -1,25 +0,0 @@
1
- import { Schema, Validator } from '../schema.js';
2
- import { ISchemaBuilder, SchemaBuilder } from './SchemaBuilder.js';
3
- export interface IFunctionSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false> extends ISchemaBuilder<TRequired, TNullable> {
4
- optional(): IFunctionSchemaBuilder<false, TNullable>;
5
- required(): IFunctionSchemaBuilder<true, TNullable>;
6
- nullable(): IFunctionSchemaBuilder<TRequired, true>;
7
- notNullable(): IFunctionSchemaBuilder<TRequired, false>;
8
- }
9
- export declare class FunctionSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false> extends SchemaBuilder<TRequired, TNullable> implements IFunctionSchemaBuilder<TRequired, TNullable> {
10
- protected readonly type = "function";
11
- get _schema(): Schema;
12
- clone(): this;
13
- static create<TRequired extends boolean = true, TNullable extends boolean = false>(obj?: {
14
- isRequired: TRequired;
15
- isNullable: TNullable;
16
- validators?: Validator[];
17
- preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
18
- }): IFunctionSchemaBuilder<TRequired, TNullable>;
19
- private constructor();
20
- optional(): IFunctionSchemaBuilder<false, TNullable>;
21
- required(): IFunctionSchemaBuilder<true, TNullable>;
22
- nullable(): IFunctionSchemaBuilder<TRequired, true>;
23
- notNullable(): IFunctionSchemaBuilder<TRequired, false>;
24
- }
25
- export declare const func: () => IFunctionSchemaBuilder;