@cleverbrush/schema 0.0.16 → 1.0.0-beta.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 +224 -149
- package/dist/builders/AliasSchemaBuilder.d.ts +14 -0
- package/dist/builders/AliasSchemaBuilder.js +91 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +15 -0
- package/dist/builders/ArraySchemaBuilder.js +141 -0
- package/dist/builders/BooleanSchemaBuilder.d.ts +31 -0
- package/dist/builders/BooleanSchemaBuilder.js +90 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +25 -0
- package/dist/builders/FunctionSchemaBuilder.js +66 -0
- package/dist/builders/NumberSchemaBuilder.d.ts +61 -0
- package/dist/builders/NumberSchemaBuilder.js +206 -0
- package/dist/builders/ObjectSchemaBuilder.d.ts +83 -0
- package/dist/builders/ObjectSchemaBuilder.js +344 -0
- package/dist/builders/SchemaBuilder.d.ts +36 -0
- package/dist/builders/SchemaBuilder.js +88 -0
- package/dist/builders/StringSchemaBuilder.d.ts +14 -0
- package/dist/builders/StringSchemaBuilder.js +140 -0
- package/dist/builders/UnionSchemaBuilder.d.ts +10 -0
- package/dist/builders/UnionSchemaBuilder.js +100 -0
- package/dist/defaultSchemas.d.ts +4 -0
- package/dist/defaultSchemas.js +45 -0
- package/dist/index.d.ts +38 -90
- package/dist/index.js +30 -4
- package/dist/schema.d.ts +242 -0
- package/dist/schema.js +2 -0
- package/dist/schemaRegistry.d.ts +54 -0
- package/dist/schemaRegistry.js +301 -0
- package/dist/validators/validateArray.d.ts +3 -2
- package/dist/validators/validateBoolean.d.ts +2 -2
- package/dist/validators/validateBoolean.js +1 -4
- package/dist/validators/validateFunction.d.ts +2 -0
- package/dist/validators/validateFunction.js +21 -0
- package/dist/validators/validateNumber.d.ts +2 -2
- package/dist/validators/validateNumber.js +1 -1
- package/dist/validators/validateObject.d.ts +3 -2
- package/dist/validators/validateObject.js +33 -11
- package/dist/validators/validateString.d.ts +2 -2
- package/dist/validators/validateString.js +1 -1
- package/dist/validators/validateUnion.d.ts +3 -0
- package/dist/validators/validateUnion.js +30 -0
- package/package.json +3 -3
- package/src/builders/AliasSchemaBuilder.test.ts +124 -0
- package/src/builders/AliasSchemaBuilder.ts +250 -0
- package/src/builders/ArraySchemaBuilder.test.ts +270 -0
- package/src/builders/ArraySchemaBuilder.ts +381 -0
- package/src/builders/BooleanSchemaBuilder.test.ts +196 -0
- package/src/builders/BooleanSchemaBuilder.ts +155 -0
- package/src/builders/FunctionSchemaBuilder.test.ts +134 -0
- package/src/builders/FunctionSchemaBuilder.ts +109 -0
- package/src/builders/NumberSchemaBuilder.test.ts +493 -0
- package/src/builders/NumberSchemaBuilder.ts +789 -0
- package/src/builders/ObjectSchemaBuilder.test.ts +657 -0
- package/src/builders/ObjectSchemaBuilder.ts +794 -0
- package/src/builders/SchemaBuilder.test.ts +73 -0
- package/src/builders/SchemaBuilder.ts +135 -0
- package/src/builders/StringSchemaBuilder.test.ts +318 -0
- package/src/builders/StringSchemaBuilder.ts +392 -0
- package/src/builders/UnionSchemaBuilder.test.ts +162 -0
- package/src/builders/UnionSchemaBuilder.ts +159 -0
- package/src/defaultSchemas.ts +44 -0
- package/src/index.ts +70 -190
- package/src/schema.ts +922 -0
- package/src/schemaRegistry.builders.test.ts +1438 -0
- package/src/{schemaValidator.test.ts → schemaRegistry.test.ts} +561 -185
- package/src/schemaRegistry.ts +532 -0
- package/src/validators/validateArray.ts +4 -7
- package/src/validators/validateBoolean.ts +2 -11
- package/src/validators/validateFunction.ts +25 -0
- package/src/validators/validateNumber.ts +2 -7
- package/src/validators/validateObject.ts +32 -21
- package/src/validators/validateString.ts +2 -7
- package/src/validators/validateUnion.ts +36 -0
- package/dist/schemaValidator.d.ts +0 -16
- package/dist/schemaValidator.js +0 -234
- package/src/schemaValidator.ts +0 -367
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { number } from './NumberSchemaBuilder.js';
|
|
2
|
+
|
|
3
|
+
test('Validators - 1', () => {
|
|
4
|
+
const schema = number();
|
|
5
|
+
expect(schema).not.toHaveProperty('validators');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
test('Validators - 2', () => {
|
|
9
|
+
const schema = number().addValidator(() => ({ valid: true }));
|
|
10
|
+
expect(schema).toHaveProperty('validators');
|
|
11
|
+
expect(schema.validators).toHaveProperty('length', 1);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('Validators - 3', () => {
|
|
15
|
+
const schema = number()
|
|
16
|
+
.addValidator(() => ({ valid: true }))
|
|
17
|
+
.addValidator(() => ({ valid: false }));
|
|
18
|
+
expect(schema).toHaveProperty('validators');
|
|
19
|
+
expect(schema.validators).toHaveProperty('length', 2);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('Validators - 4', () => {
|
|
23
|
+
const schema = number()
|
|
24
|
+
.addValidator(() => ({ valid: true }))
|
|
25
|
+
.clearValidators();
|
|
26
|
+
expect(schema).not.toHaveProperty('validators');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('Validators - 5', () => {
|
|
30
|
+
expect(() => {
|
|
31
|
+
number().addValidator('string' as any);
|
|
32
|
+
}).toThrow();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('Preprocessor - 1', () => {
|
|
36
|
+
const schema = number();
|
|
37
|
+
expect(schema).not.toHaveProperty('preprocessor');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('Preprocessor - 2', () => {
|
|
41
|
+
const schema = number().addPreprocessor(() => new Date());
|
|
42
|
+
expect(schema).toHaveProperty('preprocessor');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('Preprocessor - 3', () => {
|
|
46
|
+
const schema = number()
|
|
47
|
+
.addPreprocessor(() => new Date())
|
|
48
|
+
.clearPreprocessor();
|
|
49
|
+
expect(schema).not.toHaveProperty('preprocessor');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('Preprocessor - 4', () => {
|
|
53
|
+
const schema1 = number();
|
|
54
|
+
const schema2 = schema1.addPreprocessor(() => new Date());
|
|
55
|
+
const equal = (schema1 as any) === schema2;
|
|
56
|
+
expect(equal).toEqual(false);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('Preprocessor - 5', () => {
|
|
60
|
+
const schema1 = number().addPreprocessor(() => new Date());
|
|
61
|
+
const schema2 = schema1.clearPreprocessor();
|
|
62
|
+
const equal = (schema1 as any) === schema2;
|
|
63
|
+
expect(equal).toEqual(false);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('Preprocessor - 6', () => {
|
|
67
|
+
const schema1 = number()
|
|
68
|
+
.addPreprocessor(() => new Date())
|
|
69
|
+
.min(30);
|
|
70
|
+
const schema2 = schema1.clearPreprocessor();
|
|
71
|
+
const equal = (schema1 as any) === schema2;
|
|
72
|
+
expect(equal).toEqual(false);
|
|
73
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { Schema, Validator } from '../schema.js';
|
|
2
|
+
|
|
3
|
+
export interface ISchemaBuilder<
|
|
4
|
+
TRequired extends boolean = true,
|
|
5
|
+
TNullable extends boolean = false
|
|
6
|
+
> {
|
|
7
|
+
readonly _schema: Schema;
|
|
8
|
+
isRequired: TRequired;
|
|
9
|
+
isNullable: TNullable;
|
|
10
|
+
validators?: Validator[];
|
|
11
|
+
preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
|
|
12
|
+
addValidator(validator: Validator): this;
|
|
13
|
+
clearValidators(): this;
|
|
14
|
+
addPreprocessor(
|
|
15
|
+
preprocessor: ((value: unknown) => unknown | Promise<unknown>) | string
|
|
16
|
+
): this;
|
|
17
|
+
clearPreprocessor(): this;
|
|
18
|
+
clone(): this;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export abstract class SchemaBuilder<
|
|
22
|
+
TRequired extends boolean = true,
|
|
23
|
+
TNullable extends boolean = false
|
|
24
|
+
> implements ISchemaBuilder<TRequired, TNullable>
|
|
25
|
+
{
|
|
26
|
+
protected _isRequired: TRequired = true as any as TRequired;
|
|
27
|
+
protected _isNullable: TNullable = false as any as TNullable;
|
|
28
|
+
|
|
29
|
+
public validators?: Validator[];
|
|
30
|
+
|
|
31
|
+
public preprocessor?:
|
|
32
|
+
| ((value: unknown) => unknown | Promise<unknown>)
|
|
33
|
+
| string;
|
|
34
|
+
|
|
35
|
+
public abstract clone(): this;
|
|
36
|
+
|
|
37
|
+
public abstract get _schema(): Schema;
|
|
38
|
+
|
|
39
|
+
protected constructor(obj: {
|
|
40
|
+
isRequired?: boolean;
|
|
41
|
+
isNullable?: boolean;
|
|
42
|
+
preprocessor?:
|
|
43
|
+
| ((value: unknown) => unknown | Promise<unknown>)
|
|
44
|
+
| string;
|
|
45
|
+
validators?: Validator[];
|
|
46
|
+
}) {
|
|
47
|
+
if (typeof obj === 'object') {
|
|
48
|
+
if (typeof obj.isRequired !== 'undefined') {
|
|
49
|
+
this.isRequired = obj.isRequired;
|
|
50
|
+
}
|
|
51
|
+
if (typeof obj.isNullable !== 'undefined') {
|
|
52
|
+
this.isNullable = obj.isNullable;
|
|
53
|
+
}
|
|
54
|
+
if (Array.isArray(obj.validators)) {
|
|
55
|
+
this.validators = [...obj.validators];
|
|
56
|
+
}
|
|
57
|
+
if (typeof obj.preprocessor !== 'undefined') {
|
|
58
|
+
this.preprocessor = obj.preprocessor;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected getCommonSchema() {
|
|
64
|
+
const schemas = [];
|
|
65
|
+
if (typeof this.preprocessor !== 'undefined') {
|
|
66
|
+
schemas.push({
|
|
67
|
+
preprocessor: this.preprocessor
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
if (typeof this.isRequired !== 'undefined') {
|
|
71
|
+
schemas.push({ isRequired: this.isRequired });
|
|
72
|
+
}
|
|
73
|
+
if (typeof this.isNullable !== 'undefined') {
|
|
74
|
+
schemas.push({ isNullable: this.isNullable });
|
|
75
|
+
}
|
|
76
|
+
if (typeof this.validators !== 'undefined') {
|
|
77
|
+
schemas.push({ validators: [...this.validators] });
|
|
78
|
+
}
|
|
79
|
+
return Object.assign({}, ...schemas);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
addPreprocessor(
|
|
83
|
+
preprocessor: ((value: unknown) => unknown | Promise<unknown>) | string
|
|
84
|
+
) {
|
|
85
|
+
const result = this.clone();
|
|
86
|
+
result.preprocessor = preprocessor;
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
clearPreprocessor(): this {
|
|
91
|
+
if (typeof this.preprocessor === 'undefined') {
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const result = this.clone();
|
|
96
|
+
delete result.preprocessor;
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
addValidator(validator: Validator): this {
|
|
101
|
+
const result = this.clone();
|
|
102
|
+
if (typeof validator !== 'function')
|
|
103
|
+
throw new Error('validator should be a function');
|
|
104
|
+
|
|
105
|
+
if (Array.isArray(result.validators)) {
|
|
106
|
+
result.validators.push(validator);
|
|
107
|
+
} else {
|
|
108
|
+
result.validators = [validator];
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
clearValidators(): this {
|
|
114
|
+
if (typeof this.validators === 'undefined') return this;
|
|
115
|
+
const result = this.clone();
|
|
116
|
+
delete result.validators;
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public get isRequired(): TRequired {
|
|
121
|
+
return this._isRequired;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
protected set isRequired(val: boolean) {
|
|
125
|
+
this._isRequired = val as TRequired;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public get isNullable(): TNullable {
|
|
129
|
+
return this._isNullable;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
protected set isNullable(val: boolean) {
|
|
133
|
+
this._isNullable = val as TNullable;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { deepEqual } from '@cleverbrush/deep';
|
|
2
|
+
|
|
3
|
+
import { Schema } from '../schema.js';
|
|
4
|
+
import { string } from './StringSchemaBuilder.js';
|
|
5
|
+
|
|
6
|
+
test('Clean', () => {
|
|
7
|
+
const schema = string();
|
|
8
|
+
expect(schema).toHaveProperty('type', 'string');
|
|
9
|
+
expect(schema).toHaveProperty('isRequired', true);
|
|
10
|
+
expect(schema).toHaveProperty('isNullable', false);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('optional', () => {
|
|
14
|
+
const schema = string().optional();
|
|
15
|
+
expect(schema).toHaveProperty('type', 'string');
|
|
16
|
+
expect(schema).toHaveProperty('isRequired', false);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('optional - 2', () => {
|
|
20
|
+
const schema = string().optional().required();
|
|
21
|
+
expect(schema).toHaveProperty('type', 'string');
|
|
22
|
+
expect(schema).toHaveProperty('isRequired', true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('optional - 3', () => {
|
|
26
|
+
const schema1 = string();
|
|
27
|
+
const schema2 = schema1.optional();
|
|
28
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
29
|
+
const isRequiredEqual = (schema1 as any).isRequired !== schema2.isRequired;
|
|
30
|
+
expect(e).toEqual(false);
|
|
31
|
+
expect(isRequiredEqual).toEqual(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('optional - 4', () => {
|
|
35
|
+
const schema1 = string().optional();
|
|
36
|
+
const schema2 = schema1.optional();
|
|
37
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
38
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
39
|
+
expect(e).toEqual(true);
|
|
40
|
+
expect(isRequiredEqual).toEqual(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('optional - 5', () => {
|
|
44
|
+
const schema1 = string();
|
|
45
|
+
const schema2 = schema1.required();
|
|
46
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
47
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
48
|
+
expect(e).toEqual(true);
|
|
49
|
+
expect(isRequiredEqual).toEqual(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('optional - 6', () => {
|
|
53
|
+
const schema1 = string().optional();
|
|
54
|
+
const schema2 = schema1.required();
|
|
55
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
56
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
57
|
+
expect(e).toEqual(false);
|
|
58
|
+
expect(isRequiredEqual).toEqual(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('optional - 7', () => {
|
|
62
|
+
const schema1 = string().optional().required();
|
|
63
|
+
const schema2 = schema1.required();
|
|
64
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
65
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
66
|
+
expect(e).toEqual(true);
|
|
67
|
+
expect(isRequiredEqual).toEqual(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('nullable - 1', () => {
|
|
71
|
+
const schema = string().nullable();
|
|
72
|
+
expect(schema).toHaveProperty('type', 'string');
|
|
73
|
+
expect(schema).toHaveProperty('isNullable', true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('nullable - 2', () => {
|
|
77
|
+
const schema = string().nullable().notNullable();
|
|
78
|
+
expect(schema).toHaveProperty('type', 'string');
|
|
79
|
+
expect(schema).toHaveProperty('isNullable', false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('nullable - 3', () => {
|
|
83
|
+
const schema1 = string();
|
|
84
|
+
const schema2 = schema1.nullable();
|
|
85
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
86
|
+
const isNullableEqual = (schema1 as any).isNullable !== schema2.isNullable;
|
|
87
|
+
expect(e).toEqual(false);
|
|
88
|
+
expect(isNullableEqual).toEqual(true);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('nullable - 4', () => {
|
|
92
|
+
const schema1 = string().nullable();
|
|
93
|
+
const schema2 = schema1.nullable();
|
|
94
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
95
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
96
|
+
expect(e).toEqual(true);
|
|
97
|
+
expect(isNullableEqual).toEqual(true);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('nullable - 5', () => {
|
|
101
|
+
const schema1 = string();
|
|
102
|
+
const schema2 = schema1.nullable();
|
|
103
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
104
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
105
|
+
expect(e).toEqual(false);
|
|
106
|
+
expect(isNullableEqual).toEqual(false);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('nullable - 6', () => {
|
|
110
|
+
const schema1 = string();
|
|
111
|
+
const schema2 = schema1.notNullable();
|
|
112
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
113
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
114
|
+
expect(e).toEqual(true);
|
|
115
|
+
expect(isNullableEqual).toEqual(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('nullable - 7', () => {
|
|
119
|
+
const schema1 = string().nullable();
|
|
120
|
+
const schema2 = schema1.notNullable();
|
|
121
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
122
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
123
|
+
expect(e).toEqual(false);
|
|
124
|
+
expect(isNullableEqual).toEqual(false);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('equals - 1', () => {
|
|
128
|
+
const schema = string().equals('abc');
|
|
129
|
+
expect(schema._schema).toHaveProperty('type', 'string');
|
|
130
|
+
expect(schema._schema).toHaveProperty('equals', 'abc');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('equals - 2', () => {
|
|
134
|
+
const schema = string();
|
|
135
|
+
expect(schema._schema).toHaveProperty('type', 'string');
|
|
136
|
+
expect(schema._schema).not.toHaveProperty('equals');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('equals - 4', () => {
|
|
140
|
+
const schema = string().equals('abcd').clearEquals();
|
|
141
|
+
expect(schema._schema).toHaveProperty('type', 'string');
|
|
142
|
+
expect(schema._schema).not.toHaveProperty('equals');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('equals - 5', () => {
|
|
146
|
+
const schema1 = string();
|
|
147
|
+
const schema2 = schema1.equals('abcde');
|
|
148
|
+
const equals = schema1._schema.equals === schema2._schema.equals;
|
|
149
|
+
expect(equals).toEqual(false);
|
|
150
|
+
expect(schema1._schema).not.toHaveProperty('equals');
|
|
151
|
+
expect(schema2._schema).toHaveProperty('equals', 'abcde');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('equals - 6', () => {
|
|
155
|
+
const schema1 = string().equals('abcdef');
|
|
156
|
+
const schema2 = schema1.equals('abcdef');
|
|
157
|
+
const equals = schema1._schema.equals === schema2._schema.equals;
|
|
158
|
+
expect(equals).toEqual(true);
|
|
159
|
+
expect(schema1._schema).toHaveProperty('equals', 'abcdef');
|
|
160
|
+
expect(schema2._schema).toHaveProperty('equals', 'abcdef');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('equals - 6', () => {
|
|
164
|
+
const schema1 = string().equals('abcdefg');
|
|
165
|
+
const schema2 = schema1.clearEquals();
|
|
166
|
+
const equals = schema1._schema.equals === schema2._schema.equals;
|
|
167
|
+
expect(equals).toEqual(false);
|
|
168
|
+
expect(schema1._schema).toHaveProperty('equals', 'abcdefg');
|
|
169
|
+
expect(schema2._schema).not.toHaveProperty('equals');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('equals - 7', () => {
|
|
173
|
+
const schema1 = string().equals('abcdefgh').clearEquals();
|
|
174
|
+
const schema2 = schema1.clearEquals();
|
|
175
|
+
const equals = (schema1 as any) === schema2;
|
|
176
|
+
expect(equals).toEqual(true);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('minLength - 1', () => {
|
|
180
|
+
const schema = string();
|
|
181
|
+
expect(schema._schema).not.toHaveProperty('minLength');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test('minLength - 2', () => {
|
|
185
|
+
const schema = string().minLength(20);
|
|
186
|
+
expect(schema._schema).toHaveProperty('minLength', 20);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('minLength - 3', () => {
|
|
190
|
+
const schema = string().minLength(20).clearMinLength();
|
|
191
|
+
expect(schema._schema).not.toHaveProperty('minLength');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('minLength - 4', () => {
|
|
195
|
+
const schema1 = string();
|
|
196
|
+
const schema2 = schema1.minLength(20);
|
|
197
|
+
const equal = (schema1 as any) === schema2;
|
|
198
|
+
expect(equal).toEqual(false);
|
|
199
|
+
expect(schema1._schema).not.toHaveProperty('minLength');
|
|
200
|
+
expect(schema2._schema).toHaveProperty('minLength', 20);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test('minLength - 5', () => {
|
|
204
|
+
const schema1 = string().minLength(20);
|
|
205
|
+
const schema2 = schema1.minLength(20);
|
|
206
|
+
const equal = (schema1 as any) === schema2;
|
|
207
|
+
expect(equal).toEqual(true);
|
|
208
|
+
expect(schema1._schema).toHaveProperty('minLength', 20);
|
|
209
|
+
expect(schema2._schema).toHaveProperty('minLength', 20);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test('minLength - 6', () => {
|
|
213
|
+
const schema1 = string().minLength(20);
|
|
214
|
+
const schema2 = schema1.clearMinLength();
|
|
215
|
+
const equal = (schema1 as any) === schema2;
|
|
216
|
+
expect(equal).toEqual(false);
|
|
217
|
+
expect(schema1._schema).toHaveProperty('minLength', 20);
|
|
218
|
+
expect(schema2._schema).not.toHaveProperty('minLength');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('minLength - 7', () => {
|
|
222
|
+
const schema1 = string();
|
|
223
|
+
const schema2 = schema1.clearMinLength();
|
|
224
|
+
const equal = (schema1 as any) === schema2;
|
|
225
|
+
expect(equal).toEqual(true);
|
|
226
|
+
expect(schema1._schema).not.toHaveProperty('minLength');
|
|
227
|
+
expect(schema2._schema).not.toHaveProperty('minLength');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test('minLength - 8', () => {
|
|
231
|
+
const schema1 = string().clearMinLength();
|
|
232
|
+
const schema2 = schema1.clearMinLength();
|
|
233
|
+
const equal = (schema1 as any) === schema2;
|
|
234
|
+
expect(equal).toEqual(true);
|
|
235
|
+
expect(schema1._schema).not.toHaveProperty('minLength');
|
|
236
|
+
expect(schema2._schema).not.toHaveProperty('minLength');
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('maxLength - 1', () => {
|
|
240
|
+
const schema = string();
|
|
241
|
+
expect(schema._schema).not.toHaveProperty('maxLength');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('maxLength - 2', () => {
|
|
245
|
+
const schema = string().maxLength(30);
|
|
246
|
+
expect(schema._schema).toHaveProperty('maxLength', 30);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('maxLength - 3', () => {
|
|
250
|
+
const schema = string().maxLength(30).clearMaxLength();
|
|
251
|
+
expect(schema._schema).not.toHaveProperty('maxLength');
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test('maxLength - 4', () => {
|
|
255
|
+
const schema1 = string();
|
|
256
|
+
const schema2 = schema1.maxLength(20);
|
|
257
|
+
const equal = (schema1 as any) === schema2;
|
|
258
|
+
expect(equal).toEqual(false);
|
|
259
|
+
expect(schema1._schema).not.toHaveProperty('maxLength');
|
|
260
|
+
expect(schema2._schema).toHaveProperty('maxLength', 20);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('maxLength - 5', () => {
|
|
264
|
+
const schema1 = string().maxLength(20);
|
|
265
|
+
const schema2 = schema1.maxLength(20);
|
|
266
|
+
const equal = (schema1 as any) === schema2;
|
|
267
|
+
expect(equal).toEqual(true);
|
|
268
|
+
expect(schema1._schema).toHaveProperty('maxLength', 20);
|
|
269
|
+
expect(schema2._schema).toHaveProperty('maxLength', 20);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test('maxLength - 6', () => {
|
|
273
|
+
const schema1 = string().maxLength(20);
|
|
274
|
+
const schema2 = schema1.clearMaxLength();
|
|
275
|
+
const equal = (schema1 as any) === schema2;
|
|
276
|
+
expect(equal).toEqual(false);
|
|
277
|
+
expect(schema1._schema).toHaveProperty('maxLength', 20);
|
|
278
|
+
expect(schema2._schema).not.toHaveProperty('maxLength');
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('maxLength - 7', () => {
|
|
282
|
+
const schema1 = string();
|
|
283
|
+
const schema2 = schema1.clearMaxLength();
|
|
284
|
+
const equal = (schema1 as any) === schema2;
|
|
285
|
+
expect(equal).toEqual(true);
|
|
286
|
+
expect(schema1._schema).not.toHaveProperty('maxLength');
|
|
287
|
+
expect(schema2._schema).not.toHaveProperty('maxLength');
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test('maxLength - 8', () => {
|
|
291
|
+
const schema1 = string().clearMaxLength();
|
|
292
|
+
const schema2 = schema1.clearMaxLength();
|
|
293
|
+
const equal = (schema1 as any) === schema2;
|
|
294
|
+
expect(equal).toEqual(true);
|
|
295
|
+
expect(schema1._schema).not.toHaveProperty('maxLength');
|
|
296
|
+
expect(schema2._schema).not.toHaveProperty('maxLength');
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test('minLength,max - 1', () => {
|
|
300
|
+
const schema = string().minLength(20).maxLength(30);
|
|
301
|
+
expect(schema._schema).toHaveProperty('minLength', 20);
|
|
302
|
+
expect(schema._schema).toHaveProperty('maxLength', 30);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test('Clone', () => {
|
|
306
|
+
const schema1 = string()
|
|
307
|
+
.minLength(20)
|
|
308
|
+
.maxLength(30)
|
|
309
|
+
.addValidator(() => ({ valid: true }));
|
|
310
|
+
const schema2 = schema1.clone();
|
|
311
|
+
const equal = (schema1 as any) === schema2;
|
|
312
|
+
expect(equal).toEqual(false);
|
|
313
|
+
const equal2 = deepEqual(
|
|
314
|
+
(schema1 as any)._schema,
|
|
315
|
+
(schema2 as any)._schema
|
|
316
|
+
);
|
|
317
|
+
expect(equal2).toEqual(true);
|
|
318
|
+
});
|