@cleverbrush/schema 0.0.13 → 0.0.14
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/dist/index.d.ts +6 -2
- package/dist/schemaValidator.js +8 -1
- package/dist/validators/validateBoolean.d.ts +2 -0
- package/dist/validators/validateBoolean.js +33 -0
- package/package.json +2 -2
- package/src/index.ts +10 -0
- package/src/schemaValidator.test.ts +76 -0
- package/src/schemaValidator.ts +13 -2
- package/src/validators/validateBoolean.ts +45 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import SchemaValidator from './schemaValidator';
|
|
2
|
-
export declare type DefaultSchemaType = 'object' | 'function' | 'number' | 'string' | 'array' | 'alias';
|
|
2
|
+
export declare type DefaultSchemaType = 'object' | 'boolean' | 'function' | 'number' | 'string' | 'array' | 'alias';
|
|
3
3
|
export declare type PropType<TObj, TProp extends keyof TObj> = TObj[TProp];
|
|
4
4
|
export declare type DefaultPropertyDefinition = {
|
|
5
5
|
isRequired: boolean;
|
|
@@ -39,6 +39,10 @@ export declare type FunctionSchemaDefinition<T extends (...args: any) => any> =
|
|
|
39
39
|
type: 'function';
|
|
40
40
|
params?: ParamsValidators<T>;
|
|
41
41
|
};
|
|
42
|
+
export declare type BooleanSchemaDefinition<TObj> = Omit<SchemaDefintion<TObj>, 'type'> & {
|
|
43
|
+
type: 'boolean';
|
|
44
|
+
equals?: boolean;
|
|
45
|
+
};
|
|
42
46
|
export declare type NumberSchemaDefinition<TObj> = Omit<SchemaDefintion<TObj>, 'type'> & {
|
|
43
47
|
type: 'number';
|
|
44
48
|
min?: number;
|
|
@@ -61,7 +65,7 @@ export declare type ArraySchemaDefinition<TObj> = Omit<SchemaDefintion<TObj>, 't
|
|
|
61
65
|
minLength?: number;
|
|
62
66
|
maxLength?: number;
|
|
63
67
|
};
|
|
64
|
-
export declare type CompositeSchema<TObj> = TObj extends (...args: any) => any ? FunctionSchemaDefinition<TObj> : TObj extends number ? NumberSchemaDefinition<TObj> : TObj extends string ? StringSchemaDefinition<TObj> | 'string' : NumberSchemaDefinition<TObj> | StringSchemaDefinition<TObj> | ArraySchemaDefinition<TObj> | ObjectSchemaDefinition<TObj> | AliasSchemaDefinition<TObj>;
|
|
68
|
+
export declare type CompositeSchema<TObj> = TObj extends (...args: any) => any ? FunctionSchemaDefinition<TObj> : TObj extends number ? NumberSchemaDefinition<TObj> : TObj extends string ? StringSchemaDefinition<TObj> | 'string' : BooleanSchemaDefinition<TObj> | NumberSchemaDefinition<TObj> | StringSchemaDefinition<TObj> | ArraySchemaDefinition<TObj> | ObjectSchemaDefinition<TObj> | AliasSchemaDefinition<TObj>;
|
|
65
69
|
export declare type SingleSchema<TObj = Record<string, never>> = number | string | DefaultSchemaType | CompositeSchema<TObj>;
|
|
66
70
|
export declare type Schema<TObj> = SingleSchema<TObj> | Array<SingleSchema<TObj>>;
|
|
67
71
|
export interface ISchemaActions<K, T extends keyof K> {
|
package/dist/schemaValidator.js
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const deep_1 = require("@cleverbrush/deep");
|
|
4
4
|
const validateNumber_1 = require("./validators/validateNumber");
|
|
5
|
+
const validateBoolean_1 = require("./validators/validateBoolean");
|
|
5
6
|
const validateString_1 = require("./validators/validateString");
|
|
6
7
|
const validateArray_1 = require("./validators/validateArray");
|
|
7
8
|
const validateObject_1 = require("./validators/validateObject");
|
|
8
|
-
const defaultSchemaNames = ['string', 'number', 'date', 'array'];
|
|
9
|
+
const defaultSchemaNames = ['string', 'boolean', 'number', 'date', 'array'];
|
|
9
10
|
const defaultSchemas = {
|
|
10
11
|
number: {
|
|
11
12
|
type: 'number',
|
|
@@ -14,6 +15,11 @@ const defaultSchemas = {
|
|
|
14
15
|
ensureNotNaN: true,
|
|
15
16
|
ensureIsFinite: true
|
|
16
17
|
},
|
|
18
|
+
boolean: {
|
|
19
|
+
type: 'boolean',
|
|
20
|
+
isRequired: true,
|
|
21
|
+
isNullable: false
|
|
22
|
+
},
|
|
17
23
|
string: {
|
|
18
24
|
type: 'string',
|
|
19
25
|
isNullable: false,
|
|
@@ -30,6 +36,7 @@ const defaultSchemas = {
|
|
|
30
36
|
};
|
|
31
37
|
const defaultSchemasValidationStrategies = {
|
|
32
38
|
number: (obj, schema, validator) => (0, validateNumber_1.validateNumber)(obj, schema, validator),
|
|
39
|
+
boolean: (obj, schema, validator) => (0, validateBoolean_1.validateBoolean)(obj, schema, validator),
|
|
33
40
|
string: (obj, schema, validator) => (0, validateString_1.validateString)(obj, schema, validator),
|
|
34
41
|
array: (obj, schema, validator) => (0, validateArray_1.validateArray)(obj, schema, validator)
|
|
35
42
|
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateBoolean = void 0;
|
|
4
|
+
const validateBoolean = async (obj, schema, validator) => {
|
|
5
|
+
if (typeof obj === 'undefined' &&
|
|
6
|
+
typeof schema === 'object' &&
|
|
7
|
+
schema.isRequired === false) {
|
|
8
|
+
return {
|
|
9
|
+
valid: true
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
if (typeof obj !== 'boolean')
|
|
13
|
+
return {
|
|
14
|
+
valid: false,
|
|
15
|
+
errors: [`expected type boolean, but saw ${typeof obj}`]
|
|
16
|
+
};
|
|
17
|
+
if (typeof schema === 'boolean') {
|
|
18
|
+
return { valid: true };
|
|
19
|
+
}
|
|
20
|
+
const bool = obj;
|
|
21
|
+
if (typeof schema.equals === 'boolean') {
|
|
22
|
+
return schema.equals === bool
|
|
23
|
+
? { valid: true }
|
|
24
|
+
: {
|
|
25
|
+
valid: false,
|
|
26
|
+
errors: [`must be equal to ${schema.equals}`]
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
valid: true
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
exports.validateBoolean = validateBoolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cleverbrush/schema",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"object schema validator",
|
|
6
6
|
"schema",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"build": "tsc"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@cleverbrush/deep": "0.0.
|
|
22
|
+
"@cleverbrush/deep": "0.0.14"
|
|
23
23
|
},
|
|
24
24
|
"types": "./dist/index.d.ts"
|
|
25
25
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import SchemaValidator from './schemaValidator';
|
|
|
2
2
|
|
|
3
3
|
export type DefaultSchemaType =
|
|
4
4
|
| 'object'
|
|
5
|
+
| 'boolean'
|
|
5
6
|
| 'function'
|
|
6
7
|
| 'number'
|
|
7
8
|
| 'string'
|
|
@@ -72,6 +73,14 @@ export type FunctionSchemaDefinition<T extends (...args: any) => any> = Omit<
|
|
|
72
73
|
params?: ParamsValidators<T>;
|
|
73
74
|
};
|
|
74
75
|
|
|
76
|
+
export type BooleanSchemaDefinition<TObj> = Omit<
|
|
77
|
+
SchemaDefintion<TObj>,
|
|
78
|
+
'type'
|
|
79
|
+
> & {
|
|
80
|
+
type: 'boolean';
|
|
81
|
+
equals?: boolean;
|
|
82
|
+
};
|
|
83
|
+
|
|
75
84
|
export type NumberSchemaDefinition<TObj> = Omit<
|
|
76
85
|
SchemaDefintion<TObj>,
|
|
77
86
|
'type'
|
|
@@ -113,6 +122,7 @@ export type CompositeSchema<TObj> = TObj extends (...args: any) => any
|
|
|
113
122
|
: TObj extends string
|
|
114
123
|
? StringSchemaDefinition<TObj> | 'string'
|
|
115
124
|
:
|
|
125
|
+
| BooleanSchemaDefinition<TObj>
|
|
116
126
|
| NumberSchemaDefinition<TObj>
|
|
117
127
|
| StringSchemaDefinition<TObj>
|
|
118
128
|
| ArraySchemaDefinition<TObj>
|
|
@@ -554,6 +554,82 @@ test('Validate - number by schema - custom validators - 2', async () => {
|
|
|
554
554
|
expect(result).toHaveProperty('valid', false);
|
|
555
555
|
});
|
|
556
556
|
|
|
557
|
+
test('Validate - boolean - 1', async () => {
|
|
558
|
+
const validator = new SchemaValidator();
|
|
559
|
+
const result = await validator.validate(
|
|
560
|
+
{
|
|
561
|
+
type: 'boolean'
|
|
562
|
+
},
|
|
563
|
+
300
|
|
564
|
+
);
|
|
565
|
+
|
|
566
|
+
expect(result).toHaveProperty('valid', false);
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
test('Validate - boolean - 2', async () => {
|
|
570
|
+
const validator = new SchemaValidator();
|
|
571
|
+
const result = await validator.validate(
|
|
572
|
+
{
|
|
573
|
+
type: 'boolean'
|
|
574
|
+
},
|
|
575
|
+
true
|
|
576
|
+
);
|
|
577
|
+
|
|
578
|
+
expect(result).toHaveProperty('valid', true);
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
test('Validate - boolean - 3', async () => {
|
|
582
|
+
const validator = new SchemaValidator();
|
|
583
|
+
const result = await validator.validate(
|
|
584
|
+
{
|
|
585
|
+
type: 'boolean'
|
|
586
|
+
},
|
|
587
|
+
false
|
|
588
|
+
);
|
|
589
|
+
|
|
590
|
+
expect(result).toHaveProperty('valid', true);
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
test('Validate - boolean - 4', async () => {
|
|
594
|
+
const validator = new SchemaValidator();
|
|
595
|
+
const result = await validator.validate(
|
|
596
|
+
{
|
|
597
|
+
type: 'boolean',
|
|
598
|
+
equals: true
|
|
599
|
+
},
|
|
600
|
+
false
|
|
601
|
+
);
|
|
602
|
+
|
|
603
|
+
expect(result).toHaveProperty('valid', false);
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
test('Validate - boolean - 5', async () => {
|
|
607
|
+
const validator = new SchemaValidator();
|
|
608
|
+
const result = await validator.validate(
|
|
609
|
+
{
|
|
610
|
+
type: 'boolean',
|
|
611
|
+
equals: false
|
|
612
|
+
},
|
|
613
|
+
false
|
|
614
|
+
);
|
|
615
|
+
|
|
616
|
+
expect(result).toHaveProperty('valid', true);
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
test('Validate - boolean - 6', async () => {
|
|
620
|
+
const validator = new SchemaValidator();
|
|
621
|
+
const result = await validator.validate('boolean', false);
|
|
622
|
+
|
|
623
|
+
expect(result).toHaveProperty('valid', true);
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
test('Validate - boolean - 7', async () => {
|
|
627
|
+
const validator = new SchemaValidator();
|
|
628
|
+
const result = await validator.validate('boolean', '123');
|
|
629
|
+
|
|
630
|
+
expect(result).toHaveProperty('valid', false);
|
|
631
|
+
});
|
|
632
|
+
|
|
557
633
|
test('Validate - string - 1', async () => {
|
|
558
634
|
const validator = new SchemaValidator();
|
|
559
635
|
const result = await validator.validate('string', '12345');
|
package/src/schemaValidator.ts
CHANGED
|
@@ -13,14 +13,15 @@ import {
|
|
|
13
13
|
ISchemaValidator,
|
|
14
14
|
DefaultSchemaType,
|
|
15
15
|
ObjectSchemaDefinition,
|
|
16
|
-
|
|
16
|
+
BooleanSchemaDefinition
|
|
17
17
|
} from './index';
|
|
18
18
|
import { validateNumber } from './validators/validateNumber';
|
|
19
|
+
import { validateBoolean } from './validators/validateBoolean';
|
|
19
20
|
import { validateString } from './validators/validateString';
|
|
20
21
|
import { validateArray } from './validators/validateArray';
|
|
21
22
|
import { validateObject } from './validators/validateObject';
|
|
22
23
|
|
|
23
|
-
const defaultSchemaNames = ['string', 'number', 'date', 'array'];
|
|
24
|
+
const defaultSchemaNames = ['string', 'boolean', 'number', 'date', 'array'];
|
|
24
25
|
|
|
25
26
|
const defaultSchemas: { [key in DefaultSchemaType]?: Schema<any> } = {
|
|
26
27
|
number: {
|
|
@@ -30,6 +31,11 @@ const defaultSchemas: { [key in DefaultSchemaType]?: Schema<any> } = {
|
|
|
30
31
|
ensureNotNaN: true,
|
|
31
32
|
ensureIsFinite: true
|
|
32
33
|
},
|
|
34
|
+
boolean: {
|
|
35
|
+
type: 'boolean',
|
|
36
|
+
isRequired: true,
|
|
37
|
+
isNullable: false
|
|
38
|
+
},
|
|
33
39
|
string: {
|
|
34
40
|
type: 'string',
|
|
35
41
|
isNullable: false,
|
|
@@ -51,6 +57,11 @@ const defaultSchemasValidationStrategies = {
|
|
|
51
57
|
schema: NumberSchemaDefinition<any>,
|
|
52
58
|
validator: ISchemaValidator<any>
|
|
53
59
|
): Promise<ValidationResult> => validateNumber(obj, schema, validator),
|
|
60
|
+
boolean: (
|
|
61
|
+
obj: any,
|
|
62
|
+
schema: BooleanSchemaDefinition<any>,
|
|
63
|
+
validator: ISchemaValidator<any>
|
|
64
|
+
): Promise<ValidationResult> => validateBoolean(obj, schema, validator),
|
|
54
65
|
string: (
|
|
55
66
|
obj: any,
|
|
56
67
|
schema: StringSchemaDefinition<any>,
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ValidationResult,
|
|
3
|
+
BooleanSchemaDefinition,
|
|
4
|
+
ISchemaValidator
|
|
5
|
+
} from '../index';
|
|
6
|
+
|
|
7
|
+
export const validateBoolean = async (
|
|
8
|
+
obj: any,
|
|
9
|
+
schema: BooleanSchemaDefinition<any>,
|
|
10
|
+
validator: ISchemaValidator<any>
|
|
11
|
+
): Promise<ValidationResult> => {
|
|
12
|
+
if (
|
|
13
|
+
typeof obj === 'undefined' &&
|
|
14
|
+
typeof schema === 'object' &&
|
|
15
|
+
schema.isRequired === false
|
|
16
|
+
) {
|
|
17
|
+
return {
|
|
18
|
+
valid: true
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (typeof obj !== 'boolean')
|
|
22
|
+
return {
|
|
23
|
+
valid: false,
|
|
24
|
+
errors: [`expected type boolean, but saw ${typeof obj}`]
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
if (typeof schema === 'boolean') {
|
|
28
|
+
return { valid: true };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const bool = obj as boolean;
|
|
32
|
+
|
|
33
|
+
if (typeof schema.equals === 'boolean') {
|
|
34
|
+
return schema.equals === bool
|
|
35
|
+
? { valid: true }
|
|
36
|
+
: {
|
|
37
|
+
valid: false,
|
|
38
|
+
errors: [`must be equal to ${schema.equals}`]
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
valid: true
|
|
44
|
+
};
|
|
45
|
+
};
|