@cleverbrush/schema 1.0.0-beta.5 → 1.0.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.
Files changed (76) hide show
  1. package/README.md +118 -320
  2. package/dist/builders/AnySchemaBuilder.d.ts +76 -0
  3. package/dist/builders/AnySchemaBuilder.js +112 -0
  4. package/dist/builders/ArraySchemaBuilder.d.ts +112 -14
  5. package/dist/builders/ArraySchemaBuilder.js +254 -111
  6. package/dist/builders/BooleanSchemaBuilder.d.ts +69 -29
  7. package/dist/builders/BooleanSchemaBuilder.js +134 -74
  8. package/dist/builders/DateSchemaBuilder.d.ts +177 -0
  9. package/dist/builders/DateSchemaBuilder.js +433 -0
  10. package/dist/builders/FunctionSchemaBuilder.d.ts +59 -25
  11. package/dist/builders/FunctionSchemaBuilder.js +102 -58
  12. package/dist/builders/NumberSchemaBuilder.d.ts +159 -59
  13. package/dist/builders/NumberSchemaBuilder.js +345 -165
  14. package/dist/builders/ObjectSchemaBuilder.d.ts +357 -81
  15. package/dist/builders/ObjectSchemaBuilder.js +519 -283
  16. package/dist/builders/SchemaBuilder.d.ts +158 -34
  17. package/dist/builders/SchemaBuilder.js +258 -71
  18. package/dist/builders/StringSchemaBuilder.d.ts +138 -13
  19. package/dist/builders/StringSchemaBuilder.js +261 -115
  20. package/dist/builders/UnionSchemaBuilder.d.ts +129 -9
  21. package/dist/builders/UnionSchemaBuilder.js +196 -75
  22. package/dist/index.d.ts +20 -40
  23. package/dist/index.js +19 -32
  24. package/dist/utils/transaction.d.ts +46 -0
  25. package/dist/utils/transaction.js +178 -0
  26. package/package.json +27 -14
  27. package/dist/defaultSchemas.d.ts +0 -4
  28. package/dist/defaultSchemas.js +0 -45
  29. package/dist/schema.d.ts +0 -231
  30. package/dist/schema.js +0 -2
  31. package/dist/schemaRegistry.d.ts +0 -49
  32. package/dist/schemaRegistry.js +0 -278
  33. package/dist/validators/validateArray.d.ts +0 -3
  34. package/dist/validators/validateArray.js +0 -60
  35. package/dist/validators/validateBoolean.d.ts +0 -2
  36. package/dist/validators/validateBoolean.js +0 -30
  37. package/dist/validators/validateFunction.d.ts +0 -2
  38. package/dist/validators/validateFunction.js +0 -21
  39. package/dist/validators/validateNumber.d.ts +0 -2
  40. package/dist/validators/validateNumber.js +0 -69
  41. package/dist/validators/validateObject.d.ts +0 -3
  42. package/dist/validators/validateObject.js +0 -95
  43. package/dist/validators/validateString.d.ts +0 -2
  44. package/dist/validators/validateString.js +0 -50
  45. package/dist/validators/validateUnion.d.ts +0 -3
  46. package/dist/validators/validateUnion.js +0 -30
  47. package/src/builders/ArraySchemaBuilder.test.ts +0 -270
  48. package/src/builders/ArraySchemaBuilder.ts +0 -381
  49. package/src/builders/BooleanSchemaBuilder.test.ts +0 -196
  50. package/src/builders/BooleanSchemaBuilder.ts +0 -155
  51. package/src/builders/FunctionSchemaBuilder.test.ts +0 -145
  52. package/src/builders/FunctionSchemaBuilder.ts +0 -117
  53. package/src/builders/NumberSchemaBuilder.test.ts +0 -493
  54. package/src/builders/NumberSchemaBuilder.ts +0 -789
  55. package/src/builders/ObjectSchemaBuilder.test.ts +0 -657
  56. package/src/builders/ObjectSchemaBuilder.ts +0 -794
  57. package/src/builders/SchemaBuilder.test.ts +0 -73
  58. package/src/builders/SchemaBuilder.ts +0 -135
  59. package/src/builders/StringSchemaBuilder.test.ts +0 -318
  60. package/src/builders/StringSchemaBuilder.ts +0 -392
  61. package/src/builders/UnionSchemaBuilder.test.ts +0 -162
  62. package/src/builders/UnionSchemaBuilder.ts +0 -154
  63. package/src/defaultSchemas.ts +0 -44
  64. package/src/index.ts +0 -66
  65. package/src/schema.ts +0 -849
  66. package/src/schemaRegistry.builders.test.ts +0 -1393
  67. package/src/schemaRegistry.test.ts +0 -118
  68. package/src/schemaRegistry.ts +0 -461
  69. package/src/validators/validateArray.ts +0 -76
  70. package/src/validators/validateBoolean.ts +0 -36
  71. package/src/validators/validateFunction.ts +0 -25
  72. package/src/validators/validateNumber.ts +0 -82
  73. package/src/validators/validateObject.ts +0 -119
  74. package/src/validators/validateString.ts +0 -59
  75. package/src/validators/validateUnion.ts +0 -36
  76. package/tsconfig.json +0 -16
@@ -1,10 +1,130 @@
1
- import { ISchemaBuilder } from './SchemaBuilder.js';
2
- export interface IUnionSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TVariants extends any[] = []> extends ISchemaBuilder<TRequired, TNullable> {
3
- optional(): IUnionSchemaBuilder<false, TNullable, TVariants>;
4
- required(): IUnionSchemaBuilder<true, TNullable, TVariants>;
5
- nullable(): IUnionSchemaBuilder<TRequired, true, TVariants>;
6
- notNullable(): IUnionSchemaBuilder<TRequired, false, TVariants>;
7
- or<T>(schema: T): IUnionSchemaBuilder<TRequired, TNullable, [...TVariants, T]>;
8
- clearVariants(): IUnionSchemaBuilder<TRequired, TNullable, []>;
1
+ import { SchemaBuilder, ValidationResult, ValidationContext, InferType } from './SchemaBuilder.js';
2
+ type UnionSchemaBuilderCreateProps<T extends readonly SchemaBuilder<any, any>[], R extends boolean = true> = Partial<ReturnType<UnionSchemaBuilder<T, R>['introspect']>>;
3
+ type SchemaArrayToUnion<TArr extends readonly any[]> = TArr['length'] extends 1 ? InferType<TArr[0]> : TArr extends readonly [infer TFirst, ...infer TRest] ? InferType<TFirst> | SchemaArrayToUnion<[...TRest]> : never;
4
+ type TakeBeforeIndex<TArr extends readonly SchemaBuilder<any, any>[], TIndex extends number> = TArr extends [
5
+ ...infer TRest extends SchemaBuilder<any, any>[],
6
+ infer TLast extends SchemaBuilder<any, any>
7
+ ] ? TRest['length'] extends TIndex ? TRest : TakeBeforeIndex<TRest, TIndex> : never;
8
+ type TakeAfterIndex<TArr extends readonly SchemaBuilder<any, any>[], TIndex extends number, TAcc extends readonly SchemaBuilder<any, any>[] = []> = TArr extends [
9
+ ...infer TRest extends SchemaBuilder<any, any>[],
10
+ infer TLast extends SchemaBuilder<any, any>
11
+ ] ? TRest['length'] extends TIndex ? TAcc : TakeAfterIndex<TRest, TIndex, [TLast, ...TAcc]> : never;
12
+ type TakeExceptIndex<TArr extends readonly SchemaBuilder<any, any>[], TIndex extends number> = [...TakeBeforeIndex<TArr, TIndex>, ...TakeAfterIndex<TArr, TIndex>];
13
+ /**
14
+ * Union schema builder class. Allows to create schemas
15
+ * containing alternatives. E.g. string | number | Date.
16
+ * Use it when you want to define a schema for a value
17
+ * that can be of different types. The type of the value
18
+ * will be determined by the first schema that succeeds
19
+ * validation. Any schema type can be supplied as variant.
20
+ * Which means that you are not limited to primitive types and
21
+ * can construct complex types as well, e.g. object | array.
22
+ *
23
+ * **NOTE** this class is exported only to give opportunity to extend it
24
+ * by inheriting. It is not recommended to create an instance of this class
25
+ * directly. Use {@link union | union()} function instead.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const schema = union(string('foo')).or(string('bar'));
30
+ * const result = await schema.validate('foo');
31
+ * // result.valid === true
32
+ * // result.object === 'foo'
33
+ * ```
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * const schema = union(string('foo')).or(string('bar'));
38
+ * const result = await schema.validate('baz');
39
+ * // result.valid === false
40
+ * ```
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const schema = union(string('yes')).or(string('no')).or(number(0)).or(number(1));
45
+ * // equals to 'yes' | 'no' | 0 | 1 in TS
46
+ * const result = await schema.validate('yes');
47
+ * // result.valid === true
48
+ * // result.object === 'yes'
49
+ *
50
+ * const result2 = await schema.validate(0);
51
+ * // result2.valid === true
52
+ * // result2.object === 0
53
+ *
54
+ * const result3 = await schema.validate('baz');
55
+ * // result3.valid === false
56
+ *
57
+ * const result4 = await schema.validate(2);
58
+ * // result4.valid === false
59
+ * ```
60
+ *
61
+ * @see {@link union}
62
+ */
63
+ export declare class UnionSchemaBuilder<TOptions extends readonly SchemaBuilder<any, any>[], TRequired extends boolean = true, TExplicitType = undefined, TResult = TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType> extends SchemaBuilder<TResult, TRequired> {
64
+ #private;
65
+ static create(props: UnionSchemaBuilderCreateProps<any>): UnionSchemaBuilder<any, true, undefined, any>;
66
+ private constructor();
67
+ introspect(): {
68
+ /**
69
+ * Array of schemas participating in the union.
70
+ */
71
+ options: TOptions;
72
+ type: string;
73
+ isRequired: boolean;
74
+ preprocessors: readonly import("./SchemaBuilder.js").Preprocessor<TResult>[];
75
+ validators: readonly import("./SchemaBuilder.js").Validator<TResult>[];
76
+ };
77
+ /**
78
+ * @hidden
79
+ */
80
+ hasType<T>(notUsed?: T): UnionSchemaBuilder<TOptions, true, T>;
81
+ /**
82
+ * @hidden
83
+ */
84
+ clearHasType(): UnionSchemaBuilder<TOptions, TRequired, undefined>;
85
+ /**
86
+ * Performs validion of the union schema over `object`.
87
+ * @param context Optional `ValidationContext` settings.
88
+ */
89
+ validate(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
90
+ protected createFromProps<T extends readonly SchemaBuilder<any, any>[], TReq extends boolean>(props: UnionSchemaBuilderCreateProps<T, TReq>): this;
91
+ /**
92
+ * @hidden
93
+ */
94
+ required(): UnionSchemaBuilder<TOptions, true, TExplicitType>;
95
+ /**
96
+ * @hidden
97
+ */
98
+ optional(): UnionSchemaBuilder<TOptions, false, TExplicitType>;
99
+ /**
100
+ * Adds a new schema option described by `schema`.
101
+ * schema must be an instance of `SchemaBuilder` class ancestor.
102
+ * @param schema schema to be added as an option.
103
+ */
104
+ or<T extends SchemaBuilder<any, any>>(schema: T): UnionSchemaBuilder<[...TOptions, T], TRequired, TExplicitType>;
105
+ /**
106
+ * Removes option by its `index`. If `index` is out of bounds,
107
+ * an error is thrown.
108
+ * @param index index of the option, starting from `0`.
109
+ */
110
+ removeOption<T extends number>(index: T): UnionSchemaBuilder<TakeExceptIndex<TOptions, T>, TRequired, TExplicitType>;
111
+ /**
112
+ * Removes first option from the union schema.
113
+ */
114
+ removeFirstOption(): TOptions extends [
115
+ infer TFirst,
116
+ ...infer TRest extends SchemaBuilder<any, any>[]
117
+ ] ? UnionSchemaBuilder<TRest, TRequired, TExplicitType> : never;
118
+ /**
119
+ * Removes all options and replaces them by single `schema` option.
120
+ * Equivalent to `union(schema)` function, but could be useful in some cases.
121
+ * @param schema schema to be added as a single option to the new schema.
122
+ */
123
+ reset<T extends SchemaBuilder<any, any>>(schema: T): UnionSchemaBuilder<[T], TRequired, TExplicitType>;
9
124
  }
10
- export declare const union: <T extends ISchemaBuilder<true, false>, K extends T[], TRequired extends boolean = true, TNullable extends boolean = false>(...schemas: K) => IUnionSchemaBuilder<TRequired, TNullable, K>;
125
+ /**
126
+ * Creates a union schema.
127
+ * @param schema required and will be considered as a first option for the union shchema.
128
+ */
129
+ export declare const union: <T extends SchemaBuilder<any, any>>(schema: T) => UnionSchemaBuilder<[T], true, undefined, InferType<T>>;
130
+ export {};
@@ -1,95 +1,216 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.union = void 0;
4
- const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
5
- class UnionSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
6
- type = 'union';
7
- get _schema() {
8
- return Object.assign({
9
- type: this.type
10
- }, this.getCommonSchema(), {
11
- variants: this._variants
1
+ import { SchemaBuilder } from './SchemaBuilder.js';
2
+ /**
3
+ * Union schema builder class. Allows to create schemas
4
+ * containing alternatives. E.g. string | number | Date.
5
+ * Use it when you want to define a schema for a value
6
+ * that can be of different types. The type of the value
7
+ * will be determined by the first schema that succeeds
8
+ * validation. Any schema type can be supplied as variant.
9
+ * Which means that you are not limited to primitive types and
10
+ * can construct complex types as well, e.g. object | array.
11
+ *
12
+ * **NOTE** this class is exported only to give opportunity to extend it
13
+ * by inheriting. It is not recommended to create an instance of this class
14
+ * directly. Use {@link union | union()} function instead.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const schema = union(string('foo')).or(string('bar'));
19
+ * const result = await schema.validate('foo');
20
+ * // result.valid === true
21
+ * // result.object === 'foo'
22
+ * ```
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const schema = union(string('foo')).or(string('bar'));
27
+ * const result = await schema.validate('baz');
28
+ * // result.valid === false
29
+ * ```
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const schema = union(string('yes')).or(string('no')).or(number(0)).or(number(1));
34
+ * // equals to 'yes' | 'no' | 0 | 1 in TS
35
+ * const result = await schema.validate('yes');
36
+ * // result.valid === true
37
+ * // result.object === 'yes'
38
+ *
39
+ * const result2 = await schema.validate(0);
40
+ * // result2.valid === true
41
+ * // result2.object === 0
42
+ *
43
+ * const result3 = await schema.validate('baz');
44
+ * // result3.valid === false
45
+ *
46
+ * const result4 = await schema.validate(2);
47
+ * // result4.valid === false
48
+ * ```
49
+ *
50
+ * @see {@link union}
51
+ */
52
+ export class UnionSchemaBuilder extends SchemaBuilder {
53
+ #options;
54
+ static create(props) {
55
+ return new UnionSchemaBuilder({
56
+ type: 'union',
57
+ ...props
12
58
  });
13
59
  }
14
- clone() {
15
- return UnionSchemaBuilder.create(this._schema);
60
+ constructor(props) {
61
+ super(props);
62
+ if (Array.isArray(props.options)) {
63
+ this.#options = props.options;
64
+ }
65
+ }
66
+ introspect() {
67
+ return {
68
+ ...super.introspect(),
69
+ /**
70
+ * Array of schemas participating in the union.
71
+ */
72
+ options: this.#options
73
+ };
74
+ }
75
+ /**
76
+ * @hidden
77
+ */
78
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
79
+ hasType(notUsed) {
80
+ return this.createFromProps({
81
+ ...this.introspect()
82
+ });
16
83
  }
17
- _variants;
18
- static create(obj) {
19
- return new UnionSchemaBuilder(obj);
84
+ /**
85
+ * @hidden
86
+ */
87
+ clearHasType() {
88
+ return this.createFromProps({
89
+ ...this.introspect()
90
+ });
20
91
  }
21
- constructor(obj) {
22
- super(obj);
23
- this._variants = [];
24
- if (Array.isArray(obj.variants)) {
25
- for (let i = 0; i < obj.variants.length; i++) {
26
- this._variants.push(obj.variants[i]);
92
+ /**
93
+ * Performs validion of the union schema over `object`.
94
+ * @param context Optional `ValidationContext` settings.
95
+ */
96
+ async validate(object, context) {
97
+ const superResult = await super.preValidate(object, context);
98
+ const { valid, transaction: preValidationTransaction, context: prevalidationContext, errors } = superResult;
99
+ const { path } = prevalidationContext;
100
+ if (!valid) {
101
+ return {
102
+ valid,
103
+ errors
104
+ };
105
+ }
106
+ let { object: { validatedObject: objToValidate } } = preValidationTransaction;
107
+ if (!this.isRequired &&
108
+ (typeof objToValidate === 'undefined' || objToValidate === null)) {
109
+ return {
110
+ valid: true,
111
+ object: objToValidate
112
+ };
113
+ }
114
+ let minErrorsCount = Number.MAX_SAFE_INTEGER;
115
+ let resultingErrors = [];
116
+ for (let i = 0; i < this.#options.length; i++) {
117
+ const { valid, errors, object: validatedOption } = await this.#options[i].validate(objToValidate, {
118
+ ...prevalidationContext,
119
+ path: `${path}[option ${i}]`
120
+ });
121
+ if (valid) {
122
+ return {
123
+ valid: true,
124
+ object: validatedOption
125
+ };
126
+ }
127
+ else {
128
+ if (Array.isArray(errors) &&
129
+ errors.length > 0 &&
130
+ errors.length < minErrorsCount) {
131
+ resultingErrors = errors;
132
+ minErrorsCount = errors.length;
133
+ }
134
+ objToValidate =
135
+ preValidationTransaction.rollback().validatedObject;
27
136
  }
28
137
  }
138
+ return {
139
+ valid: false,
140
+ errors: resultingErrors
141
+ };
29
142
  }
30
- optional() {
31
- if (this.isRequired === false) {
32
- return this;
33
- }
34
- return UnionSchemaBuilder.create({
35
- ...this._schema,
36
- isRequired: false
37
- });
143
+ createFromProps(props) {
144
+ return UnionSchemaBuilder.create(props);
38
145
  }
146
+ /**
147
+ * @hidden
148
+ */
39
149
  required() {
40
- if (this.isRequired === true) {
41
- return this;
42
- }
43
- return UnionSchemaBuilder.create({
44
- ...this._schema,
45
- isRequired: true
46
- });
150
+ return super.required();
47
151
  }
48
- nullable() {
49
- if (this.isNullable === true) {
50
- return this;
152
+ /**
153
+ * @hidden
154
+ */
155
+ optional() {
156
+ return super.optional();
157
+ }
158
+ /**
159
+ * Adds a new schema option described by `schema`.
160
+ * schema must be an instance of `SchemaBuilder` class ancestor.
161
+ * @param schema schema to be added as an option.
162
+ */
163
+ or(schema) {
164
+ if (!(schema instanceof SchemaBuilder)) {
165
+ throw new Error('schema must be an instance of the SchemaBuilder class');
51
166
  }
52
- return UnionSchemaBuilder.create({
53
- ...this._schema,
54
- isNullable: true
167
+ return this.createFromProps({
168
+ ...this.introspect(),
169
+ options: [...this.#options, schema]
55
170
  });
56
171
  }
57
- notNullable() {
58
- if (this.isNullable === false) {
59
- return this;
172
+ /**
173
+ * Removes option by its `index`. If `index` is out of bounds,
174
+ * an error is thrown.
175
+ * @param index index of the option, starting from `0`.
176
+ */
177
+ removeOption(index) {
178
+ if (typeof index !== 'number' ||
179
+ index < 0 ||
180
+ index > this.#options.length) {
181
+ throw new Error('index must be >= 0 and <= count of the options');
60
182
  }
61
- return UnionSchemaBuilder.create({
62
- ...this._schema,
63
- isNullable: false
183
+ return this.createFromProps({
184
+ ...this.introspect(),
185
+ options: this.#options.filter((v, i) => i !== index)
64
186
  });
65
187
  }
66
- or(schema) {
67
- return UnionSchemaBuilder.create({
68
- ...this._schema,
69
- variants: [...this._variants, schema]
70
- });
188
+ /**
189
+ * Removes first option from the union schema.
190
+ */
191
+ removeFirstOption() {
192
+ return this.removeOption(0);
71
193
  }
72
- clearVariants() {
73
- if (this._variants.length === 0)
74
- return this;
75
- return UnionSchemaBuilder.create({
76
- ...this._schema,
77
- variants: []
194
+ /**
195
+ * Removes all options and replaces them by single `schema` option.
196
+ * Equivalent to `union(schema)` function, but could be useful in some cases.
197
+ * @param schema schema to be added as a single option to the new schema.
198
+ */
199
+ reset(schema) {
200
+ if (!(schema instanceof SchemaBuilder)) {
201
+ throw new Error('schema must be an instance of the SchemaBuilder class');
202
+ }
203
+ return this.createFromProps({
204
+ ...this.introspect(),
205
+ options: [schema]
78
206
  });
79
207
  }
80
208
  }
81
- const union = (...schemas) => {
82
- if (schemas.length < 1) {
83
- throw new Error('should provide at least one variant');
84
- }
85
- let res = UnionSchemaBuilder.create({
86
- isRequired: true,
87
- isNullable: false,
88
- variants: [schemas[0]]
89
- });
90
- for (let i = 1; i < schemas.length; i++) {
91
- res = res.or(schemas[i]);
92
- }
93
- return res;
94
- };
95
- exports.union = union;
209
+ /**
210
+ * Creates a union schema.
211
+ * @param schema required and will be considered as a first option for the union shchema.
212
+ */
213
+ export const union = (schema) => UnionSchemaBuilder.create({
214
+ isRequired: true,
215
+ options: [schema]
216
+ });
package/dist/index.d.ts CHANGED
@@ -1,40 +1,20 @@
1
- import { InferType, ExpandSchemaBuilder, ValidationResultWithObject } from './schema.js';
2
- import SchemaRegistry from './schemaRegistry.js';
3
- import { array } from './builders/ArraySchemaBuilder.js';
4
- import { boolean } from './builders/BooleanSchemaBuilder.js';
5
- import { number } from './builders/NumberSchemaBuilder.js';
6
- import { object } from './builders/ObjectSchemaBuilder.js';
7
- import { string } from './builders/StringSchemaBuilder.js';
8
- import { union } from './builders/UnionSchemaBuilder.js';
9
- import { func } from './builders/FunctionSchemaBuilder.js';
10
- export declare type GetNodes<T extends Record<string, any>, TPrefix extends string = ''> = {
11
- [k in keyof T]: k extends `${TPrefix}${infer F}.${infer S}` ? F : never;
12
- }[keyof T];
13
- export declare type GetLeafs<T extends Record<string, any>, TPrefix extends string = ''> = {
14
- [k in keyof T]: k extends `${TPrefix}${infer F}` ? F extends `${infer S}.${infer G}` ? never : F : never;
15
- }[keyof T];
16
- export declare type Unfold<TBag extends Record<string, any>, TPrefix extends string = ''> = {
17
- [k in GetLeafs<TBag, TPrefix>]: ISchemaActions<TBag[`${TPrefix}${k}`]>;
18
- } & {
19
- [k in GetNodes<TBag, TPrefix>]: Unfold<TBag, `${TPrefix}${k}.`>;
20
- };
21
- export interface ISchemaActions<S> {
22
- validate(value: any): Promise<ValidationResultWithObject<InferType<ExpandSchemaBuilder<S>>>>;
23
- schema: ExpandSchemaBuilder<S>;
24
- }
25
- export interface ISchemaRegistry<T extends Record<string, any>> {
26
- schemas: Unfold<T>;
27
- }
28
- export { SchemaRegistry };
29
- declare const _default: {
30
- SchemaRegistry: typeof SchemaRegistry;
31
- array: () => import("./builders/ArraySchemaBuilder.js").IArraySchemaBuilder<true, false, undefined, undefined, undefined>;
32
- boolean: () => import("./builders/BooleanSchemaBuilder.js").IBooleanSchemaBuilder<true, false, undefined>;
33
- number: () => import("./builders/NumberSchemaBuilder.js").INumberSchemaBuilder<true, false, undefined, undefined, undefined, true, true, undefined>;
34
- object: <T>(properties?: T) => import("./builders/ObjectSchemaBuilder.js").IObjectSchemaBuilder<true, false, true, T extends undefined ? {} : T, undefined>;
35
- string: <T_1 extends string = undefined>(equals?: T_1) => import("./builders/StringSchemaBuilder.js").IStringSchemaBuilder<true, false, undefined, undefined, T_1 extends undefined ? undefined : T_1>;
36
- union: <T_2 extends import("./builders/SchemaBuilder.js").ISchemaBuilder<true, false>, K extends T_2[], TRequired extends boolean = true, TNullable extends boolean = false>(...schemas: K) => import("./builders/UnionSchemaBuilder.js").IUnionSchemaBuilder<TRequired, TNullable, K>;
37
- func: () => import("./builders/FunctionSchemaBuilder.js").IFunctionSchemaBuilder<true, false, undefined>;
38
- };
39
- export default _default;
40
- export { array, boolean, number, object, string, union, func, InferType };
1
+ export { AnySchemaBuilder } from './builders/AnySchemaBuilder.js';
2
+ export { ArraySchemaBuilder } from './builders/ArraySchemaBuilder.js';
3
+ export { BooleanSchemaBuilder } from './builders/BooleanSchemaBuilder.js';
4
+ export { DateSchemaBuilder } from './builders/DateSchemaBuilder.js';
5
+ export { FunctionSchemaBuilder } from './builders/FunctionSchemaBuilder.js';
6
+ export { ObjectSchemaBuilder } from './builders/ObjectSchemaBuilder.js';
7
+ export { NumberSchemaBuilder } from './builders/NumberSchemaBuilder.js';
8
+ export { StringSchemaBuilder } from './builders/StringSchemaBuilder.js';
9
+ export { UnionSchemaBuilder } from './builders/UnionSchemaBuilder.js';
10
+ export { SchemaBuilder } from './builders/SchemaBuilder.js';
11
+ export { InferType, MakeOptional, MakeRequired, ValidationError, ValidationResult } from './builders/SchemaBuilder.js';
12
+ export { any } from './builders/AnySchemaBuilder.js';
13
+ export { array } from './builders/ArraySchemaBuilder.js';
14
+ export { boolean } from './builders/BooleanSchemaBuilder.js';
15
+ export { date } from './builders/DateSchemaBuilder.js';
16
+ export { func } from './builders/FunctionSchemaBuilder.js';
17
+ export { number } from './builders/NumberSchemaBuilder.js';
18
+ export { object } from './builders/ObjectSchemaBuilder.js';
19
+ export { string } from './builders/StringSchemaBuilder.js';
20
+ export { union } from './builders/UnionSchemaBuilder.js';
package/dist/index.js CHANGED
@@ -1,32 +1,19 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.func = exports.union = exports.string = exports.object = exports.number = exports.boolean = exports.array = exports.SchemaRegistry = void 0;
7
- const schemaRegistry_js_1 = __importDefault(require("./schemaRegistry.js"));
8
- exports.SchemaRegistry = schemaRegistry_js_1.default;
9
- const ArraySchemaBuilder_js_1 = require("./builders/ArraySchemaBuilder.js");
10
- Object.defineProperty(exports, "array", { enumerable: true, get: function () { return ArraySchemaBuilder_js_1.array; } });
11
- const BooleanSchemaBuilder_js_1 = require("./builders/BooleanSchemaBuilder.js");
12
- Object.defineProperty(exports, "boolean", { enumerable: true, get: function () { return BooleanSchemaBuilder_js_1.boolean; } });
13
- const NumberSchemaBuilder_js_1 = require("./builders/NumberSchemaBuilder.js");
14
- Object.defineProperty(exports, "number", { enumerable: true, get: function () { return NumberSchemaBuilder_js_1.number; } });
15
- const ObjectSchemaBuilder_js_1 = require("./builders/ObjectSchemaBuilder.js");
16
- Object.defineProperty(exports, "object", { enumerable: true, get: function () { return ObjectSchemaBuilder_js_1.object; } });
17
- const StringSchemaBuilder_js_1 = require("./builders/StringSchemaBuilder.js");
18
- Object.defineProperty(exports, "string", { enumerable: true, get: function () { return StringSchemaBuilder_js_1.string; } });
19
- const UnionSchemaBuilder_js_1 = require("./builders/UnionSchemaBuilder.js");
20
- Object.defineProperty(exports, "union", { enumerable: true, get: function () { return UnionSchemaBuilder_js_1.union; } });
21
- const FunctionSchemaBuilder_js_1 = require("./builders/FunctionSchemaBuilder.js");
22
- Object.defineProperty(exports, "func", { enumerable: true, get: function () { return FunctionSchemaBuilder_js_1.func; } });
23
- exports.default = {
24
- SchemaRegistry: schemaRegistry_js_1.default,
25
- array: ArraySchemaBuilder_js_1.array,
26
- boolean: BooleanSchemaBuilder_js_1.boolean,
27
- number: NumberSchemaBuilder_js_1.number,
28
- object: ObjectSchemaBuilder_js_1.object,
29
- string: StringSchemaBuilder_js_1.string,
30
- union: UnionSchemaBuilder_js_1.union,
31
- func: FunctionSchemaBuilder_js_1.func
32
- };
1
+ export { AnySchemaBuilder } from './builders/AnySchemaBuilder.js';
2
+ export { ArraySchemaBuilder } from './builders/ArraySchemaBuilder.js';
3
+ export { BooleanSchemaBuilder } from './builders/BooleanSchemaBuilder.js';
4
+ export { DateSchemaBuilder } from './builders/DateSchemaBuilder.js';
5
+ export { FunctionSchemaBuilder } from './builders/FunctionSchemaBuilder.js';
6
+ export { ObjectSchemaBuilder } from './builders/ObjectSchemaBuilder.js';
7
+ export { NumberSchemaBuilder } from './builders/NumberSchemaBuilder.js';
8
+ export { StringSchemaBuilder } from './builders/StringSchemaBuilder.js';
9
+ export { UnionSchemaBuilder } from './builders/UnionSchemaBuilder.js';
10
+ export { SchemaBuilder } from './builders/SchemaBuilder.js';
11
+ export { any } from './builders/AnySchemaBuilder.js';
12
+ export { array } from './builders/ArraySchemaBuilder.js';
13
+ export { boolean } from './builders/BooleanSchemaBuilder.js';
14
+ export { date } from './builders/DateSchemaBuilder.js';
15
+ export { func } from './builders/FunctionSchemaBuilder.js';
16
+ export { number } from './builders/NumberSchemaBuilder.js';
17
+ export { object } from './builders/ObjectSchemaBuilder.js';
18
+ export { string } from './builders/StringSchemaBuilder.js';
19
+ export { union } from './builders/UnionSchemaBuilder.js';
@@ -0,0 +1,46 @@
1
+ export type TransactionOptions = {
2
+ /**
3
+ * An optional callback returning boolean. Called for every
4
+ * nested object's property (recursively) which is not
5
+ * null `Object`. Using this function you can define if `child`
6
+ * should be wrapped with transaction. It is useful for
7
+ * some cases like Error, Map, etc. when you want to preserve a link,
8
+ * instead of creation of the new transaction.
9
+ * By default it's filtering all instances inheriting from Error and RegExp
10
+ * If `true` is returned from this function `child` will not be wrapped with
11
+ * nested transaction object.
12
+ */
13
+ shouldNotWrapWithTransaction?: (child: any) => boolean;
14
+ };
15
+ export type Transaction<T> = {
16
+ /**
17
+ * Transaction object you can modify (equals to `initial` right after the call).
18
+ * All changes to `object` will not be reflected to `initial` until `commit`
19
+ * is called.
20
+ */
21
+ object: T;
22
+ /**
23
+ * Commits transaction, all changes to `object` will be reflected to `initial`
24
+ * after the call of this function.
25
+ */
26
+ commit: () => T;
27
+ /**
28
+ * Rollbacks transaction moving it to the initial state (`object` will be equal to `initial`
29
+ * after the call of this function).
30
+ */
31
+ rollback: () => T;
32
+ /**
33
+ * Returns `true` if there are any changes to `object` which makes is different from `initial`
34
+ */
35
+ isDirty: () => boolean;
36
+ };
37
+ /**
38
+ * Starts transaction over the `initial` object.
39
+ */
40
+ export declare const transaction: <T extends {}>(initial: T, options?: TransactionOptions) => Transaction<T>;
41
+ /**
42
+ * Checks if `obj` is instance of transaction
43
+ * @param obj object to check if it's a transaction
44
+ * @returns
45
+ */
46
+ export declare const isTransaction: (obj: any) => any;