@orion-js/schema 3.11.8 → 4.0.0-alpha.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.
- package/dist/index.cjs +3635 -0
- package/dist/index.d.ts +185 -0
- package/dist/index.js +3607 -0
- package/package.json +19 -13
- package/LICENSE +0 -21
- package/lib/Errors.d.ts +0 -21
- package/lib/Errors.js +0 -22
- package/lib/ValidationError.d.ts +0 -14
- package/lib/ValidationError.js +0 -45
- package/lib/ValidationError.test.d.ts +0 -1
- package/lib/ValidationError.test.js +0 -35
- package/lib/clean/cleanType.d.ts +0 -3
- package/lib/clean/cleanType.js +0 -49
- package/lib/clean/getObjectNode.d.ts +0 -2
- package/lib/clean/getObjectNode.js +0 -14
- package/lib/clean/index.d.ts +0 -2
- package/lib/clean/index.js +0 -30
- package/lib/clean/index.test.d.ts +0 -1
- package/lib/clean/index.test.js +0 -482
- package/lib/clean/recursiveClean.d.ts +0 -3
- package/lib/clean/recursiveClean.js +0 -89
- package/lib/cleanKey.d.ts +0 -1
- package/lib/cleanKey.js +0 -25
- package/lib/cleanKey.test.d.ts +0 -1
- package/lib/cleanKey.test.js +0 -115
- package/lib/dotGetSchema.d.ts +0 -2
- package/lib/dotGetSchema.js +0 -7
- package/lib/fieldType.d.ts +0 -17
- package/lib/fieldType.js +0 -28
- package/lib/fieldTypes/ID.d.ts +0 -2
- package/lib/fieldTypes/ID.js +0 -29
- package/lib/fieldTypes/ID.test.d.ts +0 -1
- package/lib/fieldTypes/ID.test.js +0 -25
- package/lib/fieldTypes/any.d.ts +0 -2
- package/lib/fieldTypes/any.js +0 -10
- package/lib/fieldTypes/any.test.d.ts +0 -1
- package/lib/fieldTypes/any.test.js +0 -13
- package/lib/fieldTypes/array.d.ts +0 -2
- package/lib/fieldTypes/array.js +0 -23
- package/lib/fieldTypes/array.test.d.ts +0 -1
- package/lib/fieldTypes/array.test.js +0 -22
- package/lib/fieldTypes/blackbox.d.ts +0 -2
- package/lib/fieldTypes/blackbox.js +0 -15
- package/lib/fieldTypes/blackbox.test.d.ts +0 -1
- package/lib/fieldTypes/blackbox.test.js +0 -16
- package/lib/fieldTypes/boolean.d.ts +0 -2
- package/lib/fieldTypes/boolean.js +0 -36
- package/lib/fieldTypes/boolean.test.d.ts +0 -1
- package/lib/fieldTypes/boolean.test.js +0 -27
- package/lib/fieldTypes/date.d.ts +0 -2
- package/lib/fieldTypes/date.js +0 -36
- package/lib/fieldTypes/date.test.d.ts +0 -1
- package/lib/fieldTypes/date.test.js +0 -37
- package/lib/fieldTypes/email.d.ts +0 -2
- package/lib/fieldTypes/email.js +0 -29
- package/lib/fieldTypes/email.test.d.ts +0 -1
- package/lib/fieldTypes/email.test.js +0 -20
- package/lib/fieldTypes/enum.d.ts +0 -4
- package/lib/fieldTypes/enum.js +0 -57
- package/lib/fieldTypes/enum.test.d.ts +0 -1
- package/lib/fieldTypes/enum.test.js +0 -41
- package/lib/fieldTypes/index.d.ts +0 -14
- package/lib/fieldTypes/index.js +0 -29
- package/lib/fieldTypes/integer.d.ts +0 -2
- package/lib/fieldTypes/integer.js +0 -17
- package/lib/fieldTypes/integer.test.d.ts +0 -1
- package/lib/fieldTypes/integer.test.js +0 -19
- package/lib/fieldTypes/number.d.ts +0 -2
- package/lib/fieldTypes/number.js +0 -32
- package/lib/fieldTypes/number.test.d.ts +0 -1
- package/lib/fieldTypes/number.test.js +0 -28
- package/lib/fieldTypes/plainObject.d.ts +0 -2
- package/lib/fieldTypes/plainObject.js +0 -29
- package/lib/fieldTypes/plainObject.test.d.ts +0 -1
- package/lib/fieldTypes/plainObject.test.js +0 -42
- package/lib/fieldTypes/string.d.ts +0 -2
- package/lib/fieldTypes/string.js +0 -47
- package/lib/fieldTypes/string.test.d.ts +0 -1
- package/lib/fieldTypes/string.test.js +0 -28
- package/lib/getSchemaFromTypedModel.d.ts +0 -2
- package/lib/getSchemaFromTypedModel.js +0 -15
- package/lib/getValidationErrors/convertTypedModel.d.ts +0 -3
- package/lib/getValidationErrors/convertTypedModel.js +0 -33
- package/lib/getValidationErrors/doValidation.d.ts +0 -2
- package/lib/getValidationErrors/doValidation.js +0 -78
- package/lib/getValidationErrors/getError/getFieldType.d.ts +0 -4
- package/lib/getValidationErrors/getError/getFieldType.js +0 -13
- package/lib/getValidationErrors/getError/getFieldValidator.d.ts +0 -2
- package/lib/getValidationErrors/getError/getFieldValidator.js +0 -38
- package/lib/getValidationErrors/getError/getFieldValidator.test.d.ts +0 -1
- package/lib/getValidationErrors/getError/getFieldValidator.test.js +0 -72
- package/lib/getValidationErrors/getError/index.d.ts +0 -2
- package/lib/getValidationErrors/getError/index.js +0 -44
- package/lib/getValidationErrors/getError/index.test.d.ts +0 -1
- package/lib/getValidationErrors/getError/index.test.js +0 -52
- package/lib/getValidationErrors/getValidationErrorsObject.d.ts +0 -4
- package/lib/getValidationErrors/getValidationErrorsObject.js +0 -17
- package/lib/getValidationErrors/index.d.ts +0 -2
- package/lib/getValidationErrors/index.js +0 -34
- package/lib/getValidationErrors/index.test.d.ts +0 -1
- package/lib/getValidationErrors/index.test.js +0 -416
- package/lib/index.d.ts +0 -13
- package/lib/index.js +0 -42
- package/lib/isValid.d.ts +0 -2
- package/lib/isValid.js +0 -11
- package/lib/isValid.test.d.ts +0 -1
- package/lib/isValid.test.js +0 -25
- package/lib/types/fieldValidators.d.ts +0 -2
- package/lib/types/fieldValidators.js +0 -2
- package/lib/types/index.d.ts +0 -2
- package/lib/types/index.js +0 -18
- package/lib/types/schema.d.ts +0 -118
- package/lib/types/schema.js +0 -3
- package/lib/types/types.test.d.ts +0 -1
- package/lib/types/types.test.js +0 -17
- package/lib/validate.d.ts +0 -2
- package/lib/validate.js +0 -14
- package/lib/validate.test.d.ts +0 -1
- package/lib/validate.test.js +0 -29
- package/lib/validateKey/dotGetSchema.d.ts +0 -2
- package/lib/validateKey/dotGetSchema.js +0 -32
- package/lib/validateKey/dotGetSchema.test.d.ts +0 -1
- package/lib/validateKey/dotGetSchema.test.js +0 -65
- package/lib/validateKey/index.d.ts +0 -2
- package/lib/validateKey/index.js +0 -31
- package/lib/validateKey/index.test.d.ts +0 -1
- package/lib/validateKey/index.test.js +0 -84
|
@@ -1,44 +0,0 @@
|
|
|
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
|
-
const isNil_1 = __importDefault(require("lodash/isNil"));
|
|
7
|
-
const getFieldValidator_1 = __importDefault(require("./getFieldValidator"));
|
|
8
|
-
const fieldTypes_1 = __importDefault(require("../../fieldTypes"));
|
|
9
|
-
const Errors_1 = __importDefault(require("../../Errors"));
|
|
10
|
-
async function getValidationErrors(params) {
|
|
11
|
-
const { schema, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
|
|
12
|
-
const info = { schema, doc, currentDoc, keys, currentSchema, options };
|
|
13
|
-
if ((0, isNil_1.default)(value)) {
|
|
14
|
-
if (!currentSchema.optional && !options.omitRequired) {
|
|
15
|
-
return Errors_1.default.REQUIRED;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
const validatorKey = (0, getFieldValidator_1.default)(currentSchema.type);
|
|
20
|
-
const validator = validatorKey === 'custom' ? currentSchema.type : fieldTypes_1.default[validatorKey];
|
|
21
|
-
const error = await validator.validate(value, info, ...args);
|
|
22
|
-
if (error) {
|
|
23
|
-
return error;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
// to not deprecate yet custom field
|
|
27
|
-
if (currentSchema.custom)
|
|
28
|
-
currentSchema.validate = currentSchema.custom;
|
|
29
|
-
if (currentSchema.validate) {
|
|
30
|
-
const customError = await currentSchema.validate(value, info, ...args);
|
|
31
|
-
if (customError) {
|
|
32
|
-
return customError;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
const type = currentSchema.type;
|
|
36
|
-
if (type.__validate) {
|
|
37
|
-
const typeError = await type.__validate(value, info, ...args);
|
|
38
|
-
if (typeError) {
|
|
39
|
-
return typeError;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
exports.default = getValidationErrors;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,52 +0,0 @@
|
|
|
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
|
-
const index_1 = __importDefault(require("./index"));
|
|
7
|
-
const Errors_1 = __importDefault(require("../../Errors"));
|
|
8
|
-
const fieldType_1 = __importDefault(require("../../fieldType"));
|
|
9
|
-
test('pass a simple string validation', async () => {
|
|
10
|
-
const error = await (0, index_1.default)({ value: 'A valid string', currentSchema: { type: String } });
|
|
11
|
-
expect(error).toBeNull();
|
|
12
|
-
});
|
|
13
|
-
test('detect required field when value is null', async () => {
|
|
14
|
-
const error = await (0, index_1.default)({ value: null, currentSchema: { type: String } });
|
|
15
|
-
expect(error).toBe(Errors_1.default.REQUIRED);
|
|
16
|
-
});
|
|
17
|
-
test('dont detect required field when value is null and omit required is passed', async () => {
|
|
18
|
-
const error = await (0, index_1.default)({
|
|
19
|
-
options: { omitRequired: true },
|
|
20
|
-
value: null,
|
|
21
|
-
currentSchema: { type: String }
|
|
22
|
-
});
|
|
23
|
-
expect(error).toBeNull();
|
|
24
|
-
});
|
|
25
|
-
test('run custom validation if passed', async () => {
|
|
26
|
-
const customType = (0, fieldType_1.default)({
|
|
27
|
-
name: 'customValidator',
|
|
28
|
-
validate(value) {
|
|
29
|
-
return 'hello';
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
const error = await (0, index_1.default)({
|
|
33
|
-
value: 'A string',
|
|
34
|
-
currentSchema: { type: customType }
|
|
35
|
-
});
|
|
36
|
-
expect(error).toBe('hello');
|
|
37
|
-
});
|
|
38
|
-
test('run async custom validation', async () => {
|
|
39
|
-
const customType = (0, fieldType_1.default)({
|
|
40
|
-
name: 'customAsyncValidator',
|
|
41
|
-
async validate(value) {
|
|
42
|
-
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
|
|
43
|
-
await sleep(1);
|
|
44
|
-
return 'hello';
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
const error = await (0, index_1.default)({
|
|
48
|
-
value: 'A string',
|
|
49
|
-
currentSchema: { type: customType }
|
|
50
|
-
});
|
|
51
|
-
expect(error).toBe('hello');
|
|
52
|
-
});
|
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
const dot_object_1 = __importDefault(require("dot-object"));
|
|
7
|
-
const dot = new dot_object_1.default('.', false, true, false);
|
|
8
|
-
function getValidationErrorsObject(validationErrors) {
|
|
9
|
-
if (validationErrors.length === 0)
|
|
10
|
-
return null;
|
|
11
|
-
const errors = {};
|
|
12
|
-
for (const validationError of validationErrors) {
|
|
13
|
-
errors[validationError.key] = validationError.code;
|
|
14
|
-
}
|
|
15
|
-
return dot.dot(errors);
|
|
16
|
-
}
|
|
17
|
-
exports.default = getValidationErrorsObject;
|
|
@@ -1,34 +0,0 @@
|
|
|
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
|
-
const getSchemaFromTypedModel_1 = require("../getSchemaFromTypedModel");
|
|
7
|
-
const doValidation_1 = __importDefault(require("./doValidation"));
|
|
8
|
-
const getValidationErrorsObject_1 = __importDefault(require("./getValidationErrorsObject"));
|
|
9
|
-
const defaultOptions = {
|
|
10
|
-
omitRequired: false
|
|
11
|
-
};
|
|
12
|
-
async function getValidationErrors(schema, doc, passedOptions = {}, ...args) {
|
|
13
|
-
schema = (0, getSchemaFromTypedModel_1.getSchemaFromTypedModel)(schema);
|
|
14
|
-
const options = { ...defaultOptions, ...passedOptions };
|
|
15
|
-
const errors = [];
|
|
16
|
-
const addError = function (keys, code) {
|
|
17
|
-
errors.push({
|
|
18
|
-
key: keys.join('.'),
|
|
19
|
-
code
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
await (0, doValidation_1.default)({
|
|
23
|
-
schema,
|
|
24
|
-
doc,
|
|
25
|
-
currentDoc: doc,
|
|
26
|
-
value: doc,
|
|
27
|
-
currentSchema: { type: schema },
|
|
28
|
-
addError,
|
|
29
|
-
options,
|
|
30
|
-
args
|
|
31
|
-
});
|
|
32
|
-
return (0, getValidationErrorsObject_1.default)(errors);
|
|
33
|
-
}
|
|
34
|
-
exports.default = getValidationErrors;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,416 +0,0 @@
|
|
|
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
|
-
const index_1 = __importDefault(require("./index"));
|
|
7
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
8
|
-
const friend = {
|
|
9
|
-
firstName: {
|
|
10
|
-
type: String
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
const car = {
|
|
14
|
-
brand: {
|
|
15
|
-
type: String
|
|
16
|
-
},
|
|
17
|
-
model: {
|
|
18
|
-
type: String
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
const schema = {
|
|
22
|
-
firstName: {
|
|
23
|
-
type: String
|
|
24
|
-
},
|
|
25
|
-
lastName: {
|
|
26
|
-
type: String,
|
|
27
|
-
optional: true
|
|
28
|
-
},
|
|
29
|
-
friends: {
|
|
30
|
-
type: [friend]
|
|
31
|
-
},
|
|
32
|
-
car: {
|
|
33
|
-
type: car,
|
|
34
|
-
optional: true
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
test('returns null when object is valid', async () => {
|
|
38
|
-
const errors = await (0, index_1.default)(schema, {
|
|
39
|
-
firstName: 'Nicolás',
|
|
40
|
-
lastName: 'López',
|
|
41
|
-
friends: [{ firstName: 'Roberto' }, { firstName: 'Joaquin' }],
|
|
42
|
-
car: {
|
|
43
|
-
brand: 'Nissan',
|
|
44
|
-
model: 'Kicks'
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
expect(errors).toBeNull();
|
|
48
|
-
});
|
|
49
|
-
test('returns an array with the respective errors', async () => {
|
|
50
|
-
const errors = await (0, index_1.default)(schema, {
|
|
51
|
-
lastName: 'López',
|
|
52
|
-
friends: [{ lastName: 'Zibert' }, { firstName: 'Joaquin' }]
|
|
53
|
-
});
|
|
54
|
-
expect(errors).toEqual({
|
|
55
|
-
firstName: Errors_1.default.REQUIRED,
|
|
56
|
-
'friends.0.firstName': Errors_1.default.REQUIRED,
|
|
57
|
-
'friends.0.lastName': Errors_1.default.NOT_IN_SCHEMA
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
test('omits required if specified', async () => {
|
|
61
|
-
const errors = await (0, index_1.default)(schema, {
|
|
62
|
-
lastName: 'López',
|
|
63
|
-
friends: [{}, { firstName: 'Joaquin' }]
|
|
64
|
-
}, { omitRequired: true });
|
|
65
|
-
expect(errors).toBeNull();
|
|
66
|
-
});
|
|
67
|
-
test('gives error when a document field is not present in schema', async () => {
|
|
68
|
-
const errors = await (0, index_1.default)({
|
|
69
|
-
name: {
|
|
70
|
-
type: String
|
|
71
|
-
}
|
|
72
|
-
}, {
|
|
73
|
-
name: 'Nicolás López',
|
|
74
|
-
age: 25
|
|
75
|
-
});
|
|
76
|
-
expect(errors).toEqual({
|
|
77
|
-
age: Errors_1.default.NOT_IN_SCHEMA
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
test('dont give error when array is optional and its not passed', async () => {
|
|
81
|
-
const schema = {
|
|
82
|
-
name: {
|
|
83
|
-
type: String
|
|
84
|
-
},
|
|
85
|
-
tags: {
|
|
86
|
-
type: [String],
|
|
87
|
-
optional: true
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
const errors = await (0, index_1.default)(schema, {
|
|
91
|
-
name: 'Nicolás López',
|
|
92
|
-
tags: []
|
|
93
|
-
});
|
|
94
|
-
expect(errors).toBeNull();
|
|
95
|
-
});
|
|
96
|
-
test('gives an error if a optional object is present and has missing values', async () => {
|
|
97
|
-
const errors = await (0, index_1.default)(schema, {
|
|
98
|
-
firstName: 'Nicolás',
|
|
99
|
-
lastName: 'López',
|
|
100
|
-
friends: [{ firstName: 'Joaquin' }],
|
|
101
|
-
car: {
|
|
102
|
-
brand: 'Nissan'
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
expect(errors).toEqual({
|
|
106
|
-
'car.model': Errors_1.default.REQUIRED
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
test('can check errors in deeply nested keys', async () => {
|
|
110
|
-
const children = {
|
|
111
|
-
name: {
|
|
112
|
-
type: String
|
|
113
|
-
},
|
|
114
|
-
car: {
|
|
115
|
-
type: car,
|
|
116
|
-
optional: true
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
const mother = {
|
|
120
|
-
name: {
|
|
121
|
-
type: String
|
|
122
|
-
},
|
|
123
|
-
children: {
|
|
124
|
-
type: [children]
|
|
125
|
-
},
|
|
126
|
-
car: {
|
|
127
|
-
type: car
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
const family = {
|
|
131
|
-
name: {
|
|
132
|
-
type: String
|
|
133
|
-
},
|
|
134
|
-
mother: {
|
|
135
|
-
type: mother
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
const deepSchema = {
|
|
139
|
-
name: {
|
|
140
|
-
type: String
|
|
141
|
-
},
|
|
142
|
-
family: {
|
|
143
|
-
type: family
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
const errors = await (0, index_1.default)(deepSchema, {
|
|
147
|
-
name: 'Nicolás',
|
|
148
|
-
family: {
|
|
149
|
-
name: 'López',
|
|
150
|
-
mother: {
|
|
151
|
-
name: 'Paula',
|
|
152
|
-
car: {
|
|
153
|
-
model: 'Wrangler'
|
|
154
|
-
},
|
|
155
|
-
children: [
|
|
156
|
-
{
|
|
157
|
-
name: 'Paula',
|
|
158
|
-
car: {
|
|
159
|
-
brand: 'Citroën'
|
|
160
|
-
}
|
|
161
|
-
},
|
|
162
|
-
{
|
|
163
|
-
name: 'Diego',
|
|
164
|
-
age: 21
|
|
165
|
-
}
|
|
166
|
-
]
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
expect(errors).toEqual({
|
|
171
|
-
'family.mother.car.brand': Errors_1.default.REQUIRED,
|
|
172
|
-
'family.mother.children.0.car.model': Errors_1.default.REQUIRED,
|
|
173
|
-
'family.mother.children.1.age': Errors_1.default.NOT_IN_SCHEMA
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
test('run validate validation when field is optional and no value is passed', async () => {
|
|
177
|
-
const person = {
|
|
178
|
-
name: {
|
|
179
|
-
type: String,
|
|
180
|
-
optional: true,
|
|
181
|
-
validate(value) {
|
|
182
|
-
return 'No';
|
|
183
|
-
}
|
|
184
|
-
},
|
|
185
|
-
__validate: async (value) => {
|
|
186
|
-
if (!value)
|
|
187
|
-
return 'No object';
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
const schema = {
|
|
191
|
-
person: {
|
|
192
|
-
type: person,
|
|
193
|
-
optional: true
|
|
194
|
-
},
|
|
195
|
-
number: {
|
|
196
|
-
type: Number,
|
|
197
|
-
validate() {
|
|
198
|
-
return 'No';
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
expect(await (0, index_1.default)(schema, {
|
|
203
|
-
person: { name: null }
|
|
204
|
-
})).toEqual({
|
|
205
|
-
number: Errors_1.default.REQUIRED,
|
|
206
|
-
'person.name': 'No'
|
|
207
|
-
});
|
|
208
|
-
expect(await (0, index_1.default)(schema, {
|
|
209
|
-
person: null,
|
|
210
|
-
number: null
|
|
211
|
-
})).toEqual({
|
|
212
|
-
number: Errors_1.default.REQUIRED,
|
|
213
|
-
person: 'No object'
|
|
214
|
-
});
|
|
215
|
-
expect(await (0, index_1.default)(schema, {
|
|
216
|
-
number: 123
|
|
217
|
-
})).toEqual({
|
|
218
|
-
number: 'No',
|
|
219
|
-
person: 'No object'
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
test('can validate object type with validate validation', async () => {
|
|
223
|
-
const person = {
|
|
224
|
-
name: {
|
|
225
|
-
type: String
|
|
226
|
-
},
|
|
227
|
-
async __validate(value) {
|
|
228
|
-
return value.name === 'Nicolás' ? null : 'no';
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
const schema = {
|
|
232
|
-
person: {
|
|
233
|
-
type: person
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
expect(await (0, index_1.default)(schema, {
|
|
237
|
-
person: { name: 'Nicolás' }
|
|
238
|
-
})).toBeNull();
|
|
239
|
-
expect(await (0, index_1.default)(schema, {
|
|
240
|
-
person: { name: 'Joaquin' }
|
|
241
|
-
})).toEqual({
|
|
242
|
-
person: 'no'
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
test('skip child validation if specified', async () => {
|
|
246
|
-
const person = {
|
|
247
|
-
firstName: {
|
|
248
|
-
type: String
|
|
249
|
-
},
|
|
250
|
-
lastName: {
|
|
251
|
-
type: String
|
|
252
|
-
},
|
|
253
|
-
async __skipChildValidation(value) {
|
|
254
|
-
return value.firstName === 'Nicolás';
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
const schema = {
|
|
258
|
-
persons: {
|
|
259
|
-
type: [person]
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
expect(await (0, index_1.default)(schema, {
|
|
263
|
-
persons: [{ firstName: 'Nicolás' }]
|
|
264
|
-
})).toBeNull();
|
|
265
|
-
const errors = await (0, index_1.default)(schema, {
|
|
266
|
-
persons: [{ firstName: 'Joaquin' }]
|
|
267
|
-
});
|
|
268
|
-
expect(errors).toEqual({
|
|
269
|
-
'persons.0.lastName': Errors_1.default.REQUIRED
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
test('run custom validation with custom key', async () => {
|
|
273
|
-
const schema = {
|
|
274
|
-
name: {
|
|
275
|
-
type: String,
|
|
276
|
-
async custom(value) {
|
|
277
|
-
return value;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
const errors = await (0, index_1.default)(schema, { name: 'error' });
|
|
282
|
-
expect(errors).toEqual({
|
|
283
|
-
name: 'error'
|
|
284
|
-
});
|
|
285
|
-
});
|
|
286
|
-
test('allow custom validation to pass an error object', async () => {
|
|
287
|
-
const schema = {
|
|
288
|
-
person: {
|
|
289
|
-
type: 'blackbox',
|
|
290
|
-
async validate(person) {
|
|
291
|
-
return {
|
|
292
|
-
name: 'required',
|
|
293
|
-
lastName: 'tooShort'
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
const errors = await (0, index_1.default)(schema, { person: { lastName: 'López' } });
|
|
299
|
-
expect(errors).toEqual({
|
|
300
|
-
'person.name': 'required',
|
|
301
|
-
'person.lastName': 'tooShort'
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
test('allow to set type any', async () => {
|
|
305
|
-
const schema = {
|
|
306
|
-
person: {
|
|
307
|
-
type: 'any'
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
const errors = await (0, index_1.default)(schema, { person: { lastName: 'López' } });
|
|
311
|
-
expect(errors).toEqual(null);
|
|
312
|
-
});
|
|
313
|
-
test('allow custom validation to pass an complex error object', async () => {
|
|
314
|
-
const schema = {
|
|
315
|
-
person: {
|
|
316
|
-
type: 'blackbox',
|
|
317
|
-
async validate(person) {
|
|
318
|
-
return {
|
|
319
|
-
name: 'required',
|
|
320
|
-
'car.name': 'required',
|
|
321
|
-
friends: [{ name: 'required' }]
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
const errors = await (0, index_1.default)(schema, { person: { lastName: 'López' } });
|
|
327
|
-
expect(errors).toEqual({
|
|
328
|
-
'person.name': 'required',
|
|
329
|
-
'person.car.name': 'required',
|
|
330
|
-
'person.friends.0.name': 'required'
|
|
331
|
-
});
|
|
332
|
-
});
|
|
333
|
-
test('pass currentDoc validating arrays', async () => {
|
|
334
|
-
const aItem = { name: 'Nicolás' };
|
|
335
|
-
const doc = { items: [aItem] };
|
|
336
|
-
const item = {
|
|
337
|
-
name: {
|
|
338
|
-
type: String,
|
|
339
|
-
async validate(name, { currentDoc }) {
|
|
340
|
-
expect(currentDoc).toBe(aItem);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
};
|
|
344
|
-
const schema = {
|
|
345
|
-
items: {
|
|
346
|
-
type: [item],
|
|
347
|
-
async validate(items, { currentDoc }) {
|
|
348
|
-
expect(currentDoc).toBe(doc);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
expect.assertions(2);
|
|
353
|
-
await (0, index_1.default)(schema, doc);
|
|
354
|
-
});
|
|
355
|
-
test('pass currentDoc validating complex schemas', async () => {
|
|
356
|
-
const aCar = { brand: 'Jeep' };
|
|
357
|
-
const aMom = { name: 'Paula', car: aCar };
|
|
358
|
-
const aItem = { name: 'Nicolás', mom: aMom };
|
|
359
|
-
const doc = { items: [aItem] };
|
|
360
|
-
const car = {
|
|
361
|
-
brand: {
|
|
362
|
-
type: String,
|
|
363
|
-
async validate(value, { currentDoc }) {
|
|
364
|
-
expect(value).toEqual(aCar.brand);
|
|
365
|
-
expect(currentDoc).toEqual(aCar);
|
|
366
|
-
}
|
|
367
|
-
},
|
|
368
|
-
async __validate(value, info) {
|
|
369
|
-
expect(value).toEqual(aMom.car);
|
|
370
|
-
expect(info.currentDoc).toEqual(aMom);
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
const mom = {
|
|
374
|
-
name: {
|
|
375
|
-
type: String,
|
|
376
|
-
async validate(value, { currentDoc }) {
|
|
377
|
-
expect(value).toEqual(aMom.name);
|
|
378
|
-
expect(currentDoc).toEqual(aMom);
|
|
379
|
-
}
|
|
380
|
-
},
|
|
381
|
-
car: {
|
|
382
|
-
type: car,
|
|
383
|
-
async validate(value, { currentDoc, doc }) {
|
|
384
|
-
expect(value).toEqual(aMom.car);
|
|
385
|
-
expect(currentDoc).toEqual(aMom);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
};
|
|
389
|
-
const item = {
|
|
390
|
-
name: {
|
|
391
|
-
type: String,
|
|
392
|
-
async validate(value, { currentDoc }) {
|
|
393
|
-
expect(value).toEqual(aItem.name);
|
|
394
|
-
expect(currentDoc).toEqual(aItem);
|
|
395
|
-
}
|
|
396
|
-
},
|
|
397
|
-
mom: {
|
|
398
|
-
type: mom,
|
|
399
|
-
async validate(value, { currentDoc }) {
|
|
400
|
-
expect(value).toEqual(aItem.mom);
|
|
401
|
-
expect(currentDoc).toEqual(aItem);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
};
|
|
405
|
-
const schema = {
|
|
406
|
-
items: {
|
|
407
|
-
type: [item],
|
|
408
|
-
async validate(value, { currentDoc }) {
|
|
409
|
-
expect(value).toEqual(doc.items);
|
|
410
|
-
expect(currentDoc).toEqual(doc);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
};
|
|
414
|
-
expect.assertions(14);
|
|
415
|
-
await (0, index_1.default)(schema, doc);
|
|
416
|
-
});
|
package/lib/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import validate from './validate';
|
|
2
|
-
import ValidationError from './ValidationError';
|
|
3
|
-
import getValidationErrors from './getValidationErrors';
|
|
4
|
-
import isValid from './isValid';
|
|
5
|
-
import getFieldType from './getValidationErrors/getError/getFieldType';
|
|
6
|
-
import clean from './clean';
|
|
7
|
-
import cleanKey from './cleanKey';
|
|
8
|
-
import validateKey from './validateKey';
|
|
9
|
-
import dotGetSchema from './dotGetSchema';
|
|
10
|
-
import createEnum from './fieldTypes/enum';
|
|
11
|
-
export { validate, ValidationError, getValidationErrors, isValid, getFieldType, clean, cleanKey, dotGetSchema, validateKey, createEnum };
|
|
12
|
-
export * from './types';
|
|
13
|
-
export * from './fieldType';
|
package/lib/index.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.createEnum = exports.validateKey = exports.dotGetSchema = exports.cleanKey = exports.clean = exports.getFieldType = exports.isValid = exports.getValidationErrors = exports.ValidationError = exports.validate = void 0;
|
|
21
|
-
const validate_1 = __importDefault(require("./validate"));
|
|
22
|
-
exports.validate = validate_1.default;
|
|
23
|
-
const ValidationError_1 = __importDefault(require("./ValidationError"));
|
|
24
|
-
exports.ValidationError = ValidationError_1.default;
|
|
25
|
-
const getValidationErrors_1 = __importDefault(require("./getValidationErrors"));
|
|
26
|
-
exports.getValidationErrors = getValidationErrors_1.default;
|
|
27
|
-
const isValid_1 = __importDefault(require("./isValid"));
|
|
28
|
-
exports.isValid = isValid_1.default;
|
|
29
|
-
const getFieldType_1 = __importDefault(require("./getValidationErrors/getError/getFieldType"));
|
|
30
|
-
exports.getFieldType = getFieldType_1.default;
|
|
31
|
-
const clean_1 = __importDefault(require("./clean"));
|
|
32
|
-
exports.clean = clean_1.default;
|
|
33
|
-
const cleanKey_1 = __importDefault(require("./cleanKey"));
|
|
34
|
-
exports.cleanKey = cleanKey_1.default;
|
|
35
|
-
const validateKey_1 = __importDefault(require("./validateKey"));
|
|
36
|
-
exports.validateKey = validateKey_1.default;
|
|
37
|
-
const dotGetSchema_1 = __importDefault(require("./dotGetSchema"));
|
|
38
|
-
exports.dotGetSchema = dotGetSchema_1.default;
|
|
39
|
-
const enum_1 = __importDefault(require("./fieldTypes/enum"));
|
|
40
|
-
exports.createEnum = enum_1.default;
|
|
41
|
-
__exportStar(require("./types"), exports);
|
|
42
|
-
__exportStar(require("./fieldType"), exports);
|
package/lib/isValid.d.ts
DELETED
package/lib/isValid.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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
|
-
const getValidationErrors_1 = __importDefault(require("./getValidationErrors"));
|
|
7
|
-
async function isValid(schema, doc, passedOptions = {}, ...args) {
|
|
8
|
-
const validationErrors = await (0, getValidationErrors_1.default)(schema, doc, passedOptions, ...args);
|
|
9
|
-
return !validationErrors;
|
|
10
|
-
}
|
|
11
|
-
exports.default = isValid;
|
package/lib/isValid.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/isValid.test.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
const isValid_1 = __importDefault(require("./isValid"));
|
|
7
|
-
const schema = {
|
|
8
|
-
firstName: {
|
|
9
|
-
type: String
|
|
10
|
-
},
|
|
11
|
-
lastName: {
|
|
12
|
-
type: String,
|
|
13
|
-
optional: true
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
const validObject = { firstName: 'Nicolás', lastName: 'López' };
|
|
17
|
-
const invalidObject = { lastName: 'López' };
|
|
18
|
-
test('returns true when object is valid', async () => {
|
|
19
|
-
const result = await (0, isValid_1.default)(schema, validObject);
|
|
20
|
-
expect(result).toBe(true);
|
|
21
|
-
});
|
|
22
|
-
test('returns false when object is invalid', async () => {
|
|
23
|
-
const result = await (0, isValid_1.default)(schema, invalidObject);
|
|
24
|
-
expect(result).toBe(false);
|
|
25
|
-
});
|