@orion-js/schema 3.11.6 → 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,41 +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 enum_1 = __importDefault(require("./enum"));
|
|
7
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
8
|
-
const getValidationErrors_1 = __importDefault(require("../getValidationErrors"));
|
|
9
|
-
test('return an error when the value is incorrect', async () => {
|
|
10
|
-
expect((0, enum_1.default)('hello', ['hello']).validate(['Hello'])).toBe(Errors_1.default.NOT_A_STRING);
|
|
11
|
-
expect((0, enum_1.default)('hello', ['hello']).validate({ name: 'Nicolás' })).toBe(Errors_1.default.NOT_A_STRING);
|
|
12
|
-
expect((0, enum_1.default)('hello', ['hello']).validate(new Date())).toBe(Errors_1.default.NOT_A_STRING);
|
|
13
|
-
});
|
|
14
|
-
test('enum typescript helpers', async () => {
|
|
15
|
-
const myEnum = (0, enum_1.default)('HelloEnum', ['hello', 'hi']);
|
|
16
|
-
const string = 'hello';
|
|
17
|
-
});
|
|
18
|
-
test('return no error when the value is correct', async () => {
|
|
19
|
-
const info = { currentSchema: { optional: true } };
|
|
20
|
-
expect((0, enum_1.default)('hello', ['hello']).validate('', info)).toBe(Errors_1.default.NOT_AN_ALLOWED_VALUE);
|
|
21
|
-
expect((0, enum_1.default)('hello', ['hello']).validate('hello')).toBeFalsy();
|
|
22
|
-
});
|
|
23
|
-
test('validate correctly allowed values', async () => {
|
|
24
|
-
expect((0, enum_1.default)('hello', ['hello']).validate('hello')).toBeFalsy();
|
|
25
|
-
expect((0, enum_1.default)('hello', ['hello']).validate('hi')).toBe(Errors_1.default.NOT_AN_ALLOWED_VALUE);
|
|
26
|
-
});
|
|
27
|
-
test('Full schema with enum', async () => {
|
|
28
|
-
const schema = {
|
|
29
|
-
color: {
|
|
30
|
-
type: (0, enum_1.default)('colors', ['red', 'blue', 'green'])
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
expect(await (0, getValidationErrors_1.default)(schema, {
|
|
34
|
-
color: 'red'
|
|
35
|
-
})).toBeNull();
|
|
36
|
-
expect(await (0, getValidationErrors_1.default)(schema, {
|
|
37
|
-
color: 'yellow'
|
|
38
|
-
})).toEqual({
|
|
39
|
-
color: Errors_1.default.NOT_AN_ALLOWED_VALUE
|
|
40
|
-
});
|
|
41
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
declare const _default: {
|
|
2
|
-
array: import("..").FieldType;
|
|
3
|
-
plainObject: import("..").FieldType;
|
|
4
|
-
string: import("..").FieldType;
|
|
5
|
-
date: import("..").FieldType;
|
|
6
|
-
integer: import("..").FieldType;
|
|
7
|
-
number: import("..").FieldType;
|
|
8
|
-
ID: import("..").FieldType;
|
|
9
|
-
boolean: import("..").FieldType;
|
|
10
|
-
email: import("..").FieldType;
|
|
11
|
-
blackbox: import("..").FieldType;
|
|
12
|
-
any: import("..").FieldType;
|
|
13
|
-
};
|
|
14
|
-
export default _default;
|
package/lib/fieldTypes/index.js
DELETED
|
@@ -1,29 +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 array_1 = __importDefault(require("./array"));
|
|
7
|
-
const plainObject_1 = __importDefault(require("./plainObject"));
|
|
8
|
-
const string_1 = __importDefault(require("./string"));
|
|
9
|
-
const date_1 = __importDefault(require("./date"));
|
|
10
|
-
const integer_1 = __importDefault(require("./integer"));
|
|
11
|
-
const number_1 = __importDefault(require("./number"));
|
|
12
|
-
const ID_1 = __importDefault(require("./ID"));
|
|
13
|
-
const boolean_1 = __importDefault(require("./boolean"));
|
|
14
|
-
const email_1 = __importDefault(require("./email"));
|
|
15
|
-
const blackbox_1 = __importDefault(require("./blackbox"));
|
|
16
|
-
const any_1 = __importDefault(require("./any"));
|
|
17
|
-
exports.default = {
|
|
18
|
-
array: array_1.default,
|
|
19
|
-
plainObject: plainObject_1.default,
|
|
20
|
-
string: string_1.default,
|
|
21
|
-
date: date_1.default,
|
|
22
|
-
integer: integer_1.default,
|
|
23
|
-
number: number_1.default,
|
|
24
|
-
ID: ID_1.default,
|
|
25
|
-
boolean: boolean_1.default,
|
|
26
|
-
email: email_1.default,
|
|
27
|
-
blackbox: blackbox_1.default,
|
|
28
|
-
any: any_1.default
|
|
29
|
-
};
|
|
@@ -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 fieldType_1 = __importDefault(require("../fieldType"));
|
|
7
|
-
const isInteger_1 = __importDefault(require("lodash/isInteger"));
|
|
8
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
9
|
-
const number_1 = __importDefault(require("./number"));
|
|
10
|
-
exports.default = (0, fieldType_1.default)({
|
|
11
|
-
name: 'integer',
|
|
12
|
-
validate(value, info) {
|
|
13
|
-
if (!(0, isInteger_1.default)(value))
|
|
14
|
-
return Errors_1.default.NOT_AN_INTEGER;
|
|
15
|
-
return number_1.default.validate(value, info);
|
|
16
|
-
}
|
|
17
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,19 +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 integer_1 = __importDefault(require("./integer"));
|
|
7
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
8
|
-
test('return an error when the value is incorrect', async () => {
|
|
9
|
-
//@ts-ignore
|
|
10
|
-
expect(integer_1.default.validate('1234')).toBe(Errors_1.default.NOT_AN_INTEGER);
|
|
11
|
-
expect(integer_1.default.validate(12.24)).toBe(Errors_1.default.NOT_AN_INTEGER);
|
|
12
|
-
expect(integer_1.default.validate(NaN)).toBe(Errors_1.default.NOT_AN_INTEGER);
|
|
13
|
-
expect(integer_1.default.validate(Infinity)).toBe(Errors_1.default.NOT_AN_INTEGER);
|
|
14
|
-
});
|
|
15
|
-
test('return no error when the value is correct', async () => {
|
|
16
|
-
expect(integer_1.default.validate(99999999999999999999999)).toBeFalsy();
|
|
17
|
-
expect(integer_1.default.validate(0)).toBeFalsy();
|
|
18
|
-
expect(integer_1.default.validate(10 * 10)).toBeFalsy();
|
|
19
|
-
});
|
package/lib/fieldTypes/number.js
DELETED
|
@@ -1,32 +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 fieldType_1 = __importDefault(require("../fieldType"));
|
|
7
|
-
const isFinite_1 = __importDefault(require("lodash/isFinite"));
|
|
8
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
9
|
-
const toNumber_1 = __importDefault(require("lodash/toNumber"));
|
|
10
|
-
exports.default = (0, fieldType_1.default)({
|
|
11
|
-
name: 'number',
|
|
12
|
-
validate(value, { currentSchema }) {
|
|
13
|
-
if (!(0, isFinite_1.default)(value))
|
|
14
|
-
return Errors_1.default.NOT_A_NUMBER;
|
|
15
|
-
if ((0, isFinite_1.default)(currentSchema.min)) {
|
|
16
|
-
if (value < currentSchema.min) {
|
|
17
|
-
return Errors_1.default.NUMBER_TOO_SMALL;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
if ((0, isFinite_1.default)(currentSchema.max)) {
|
|
21
|
-
if (value > currentSchema.max) {
|
|
22
|
-
return Errors_1.default.NUMBER_TOO_BIG;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
clean(value, { options: { autoConvert } }) {
|
|
27
|
-
if (typeof value === 'string' && autoConvert) {
|
|
28
|
-
value = (0, toNumber_1.default)(value);
|
|
29
|
-
}
|
|
30
|
-
return value;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,28 +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 number_1 = __importDefault(require("./number"));
|
|
7
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
8
|
-
test('return an error when the value is incorrect', async () => {
|
|
9
|
-
//@ts-ignore
|
|
10
|
-
expect(number_1.default.validate('1234')).toBe(Errors_1.default.NOT_A_NUMBER);
|
|
11
|
-
//@ts-ignore
|
|
12
|
-
expect(number_1.default.validate(new Date())).toBe(Errors_1.default.NOT_A_NUMBER);
|
|
13
|
-
//@ts-ignore
|
|
14
|
-
expect(number_1.default.validate([123])).toBe(Errors_1.default.NOT_A_NUMBER);
|
|
15
|
-
expect(number_1.default.validate(NaN)).toBe(Errors_1.default.NOT_A_NUMBER);
|
|
16
|
-
expect(number_1.default.validate(Infinity)).toBe(Errors_1.default.NOT_A_NUMBER);
|
|
17
|
-
});
|
|
18
|
-
test('return no error when the value is correct', async () => {
|
|
19
|
-
expect(number_1.default.validate(99999999999999999999999)).toBeFalsy();
|
|
20
|
-
expect(number_1.default.validate(10 / 3)).toBeFalsy();
|
|
21
|
-
expect(number_1.default.validate(10 * 10)).toBeFalsy();
|
|
22
|
-
});
|
|
23
|
-
test('validate correctly min and max', async () => {
|
|
24
|
-
const info = { currentSchema: { min: 0, max: 10 } };
|
|
25
|
-
expect(number_1.default.validate(10, info)).toBeFalsy();
|
|
26
|
-
expect(number_1.default.validate(-10, info)).toBe(Errors_1.default.NUMBER_TOO_SMALL);
|
|
27
|
-
expect(number_1.default.validate(10.1, info)).toBe(Errors_1.default.NUMBER_TOO_BIG);
|
|
28
|
-
});
|
|
@@ -1,29 +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 fieldType_1 = __importDefault(require("../fieldType"));
|
|
7
|
-
const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
|
|
8
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
9
|
-
const difference_1 = __importDefault(require("lodash/difference"));
|
|
10
|
-
exports.default = (0, fieldType_1.default)({
|
|
11
|
-
name: 'plainObject',
|
|
12
|
-
validate(value) {
|
|
13
|
-
if (!(0, isPlainObject_1.default)(value))
|
|
14
|
-
return Errors_1.default.NOT_AN_OBJECT;
|
|
15
|
-
},
|
|
16
|
-
clean(value, { type, options }) {
|
|
17
|
-
if (!(0, isPlainObject_1.default)(value))
|
|
18
|
-
return value;
|
|
19
|
-
if (options.filter) {
|
|
20
|
-
const documentKeys = Object.keys(value);
|
|
21
|
-
const schemaKeys = Object.keys(type);
|
|
22
|
-
const notInSchemaKeys = (0, difference_1.default)(documentKeys, schemaKeys);
|
|
23
|
-
for (const key of notInSchemaKeys) {
|
|
24
|
-
delete value[key];
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,42 +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 plainObject_1 = __importDefault(require("./plainObject"));
|
|
7
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
8
|
-
test('return an error when the value is incorrect', async () => {
|
|
9
|
-
//@ts-ignore
|
|
10
|
-
expect(plainObject_1.default.validate('a string')).toBe(Errors_1.default.NOT_AN_OBJECT);
|
|
11
|
-
expect(plainObject_1.default.validate(new Date())).toBe(Errors_1.default.NOT_AN_OBJECT);
|
|
12
|
-
expect(plainObject_1.default.validate([])).toBe(Errors_1.default.NOT_AN_OBJECT);
|
|
13
|
-
});
|
|
14
|
-
test('return no error when the value is correct', async () => {
|
|
15
|
-
expect(plainObject_1.default.validate({})).toBeFalsy();
|
|
16
|
-
expect(plainObject_1.default.validate({ name: 'Nicolás' })).toBeFalsy();
|
|
17
|
-
});
|
|
18
|
-
test('should return same value when cleaning non-object', async () => {
|
|
19
|
-
//@ts-ignore
|
|
20
|
-
expect(plainObject_1.default.clean('a string')).toBe('a string');
|
|
21
|
-
});
|
|
22
|
-
test('should filter keys not in schema', async () => {
|
|
23
|
-
const schema = {
|
|
24
|
-
yes: {
|
|
25
|
-
type: String
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
const value = {
|
|
29
|
-
yes: 'yes',
|
|
30
|
-
no: 'no'
|
|
31
|
-
};
|
|
32
|
-
expect(plainObject_1.default.clean(value, {
|
|
33
|
-
type: schema,
|
|
34
|
-
options: { filter: true }
|
|
35
|
-
})).toEqual({
|
|
36
|
-
yes: 'yes'
|
|
37
|
-
});
|
|
38
|
-
expect(plainObject_1.default.clean(value, {
|
|
39
|
-
type: schema,
|
|
40
|
-
options: { filter: false }
|
|
41
|
-
})).toEqual(value);
|
|
42
|
-
});
|
package/lib/fieldTypes/string.js
DELETED
|
@@ -1,47 +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 fieldType_1 = __importDefault(require("../fieldType"));
|
|
7
|
-
const isString_1 = __importDefault(require("lodash/isString"));
|
|
8
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
9
|
-
const includes_1 = __importDefault(require("lodash/includes"));
|
|
10
|
-
const isArray_1 = __importDefault(require("lodash/isArray"));
|
|
11
|
-
exports.default = (0, fieldType_1.default)({
|
|
12
|
-
name: 'string',
|
|
13
|
-
validate(value, { currentSchema }) {
|
|
14
|
-
if (!(0, isString_1.default)(value))
|
|
15
|
-
return Errors_1.default.NOT_A_STRING;
|
|
16
|
-
if (isFinite(currentSchema.min)) {
|
|
17
|
-
if (value.length < currentSchema.min) {
|
|
18
|
-
return Errors_1.default.STRING_TOO_SHORT;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
if (isFinite(currentSchema.max)) {
|
|
22
|
-
if (value.length > currentSchema.max) {
|
|
23
|
-
return Errors_1.default.STRING_TOO_LONG;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if ((0, isArray_1.default)(currentSchema.allowedValues)) {
|
|
27
|
-
if (!(0, includes_1.default)(currentSchema.allowedValues, value)) {
|
|
28
|
-
return Errors_1.default.NOT_AN_ALLOWED_VALUE;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
if (value === '' && !currentSchema.optional) {
|
|
32
|
-
return Errors_1.default.REQUIRED;
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
clean(value, { options: { autoConvert, trimStrings, removeEmptyStrings } }) {
|
|
36
|
-
if (autoConvert) {
|
|
37
|
-
value = String(value);
|
|
38
|
-
}
|
|
39
|
-
if (trimStrings) {
|
|
40
|
-
value = value.trim();
|
|
41
|
-
}
|
|
42
|
-
if (removeEmptyStrings && value === '') {
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
return value;
|
|
46
|
-
}
|
|
47
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,28 +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 string_1 = __importDefault(require("./string"));
|
|
7
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
8
|
-
test('return an error when the value is incorrect', async () => {
|
|
9
|
-
expect(string_1.default.validate(['Hello'])).toBe(Errors_1.default.NOT_A_STRING);
|
|
10
|
-
expect(string_1.default.validate({ name: 'Nicolás' })).toBe(Errors_1.default.NOT_A_STRING);
|
|
11
|
-
expect(string_1.default.validate(new Date())).toBe(Errors_1.default.NOT_A_STRING);
|
|
12
|
-
});
|
|
13
|
-
test('return no error when the value is correct', async () => {
|
|
14
|
-
const info = { currentSchema: { optional: true } };
|
|
15
|
-
expect(string_1.default.validate('', info)).toBeFalsy();
|
|
16
|
-
expect(string_1.default.validate('Nicolás')).toBeFalsy();
|
|
17
|
-
});
|
|
18
|
-
test('validate correctly min and max', async () => {
|
|
19
|
-
const info = { currentSchema: { min: 3, max: 5 } };
|
|
20
|
-
expect(string_1.default.validate('hello', info)).toBeFalsy();
|
|
21
|
-
expect(string_1.default.validate('hi', info)).toBe(Errors_1.default.STRING_TOO_SHORT);
|
|
22
|
-
expect(string_1.default.validate('hello my friend', info)).toBe(Errors_1.default.STRING_TOO_LONG);
|
|
23
|
-
});
|
|
24
|
-
test('validate correctly allowed values', async () => {
|
|
25
|
-
const info = { currentSchema: { allowedValues: ['hello'] } };
|
|
26
|
-
expect(string_1.default.validate('hello', info)).toBeFalsy();
|
|
27
|
-
expect(string_1.default.validate('hi', info)).toBe(Errors_1.default.NOT_AN_ALLOWED_VALUE);
|
|
28
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSchemaFromTypedModel = void 0;
|
|
4
|
-
const convertTypedModel_1 = require("./getValidationErrors/convertTypedModel");
|
|
5
|
-
const getSchemaFromTypedModel = (schema) => {
|
|
6
|
-
const item = schema;
|
|
7
|
-
if (typeof item !== 'function')
|
|
8
|
-
return item;
|
|
9
|
-
if (!(0, convertTypedModel_1.isClass)(item))
|
|
10
|
-
return item;
|
|
11
|
-
if (!item.getModel || !item.__schemaId)
|
|
12
|
-
return item;
|
|
13
|
-
return item.getModel().getCleanSchema();
|
|
14
|
-
};
|
|
15
|
-
exports.getSchemaFromTypedModel = getSchemaFromTypedModel;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertTypedModel = exports.isClass = void 0;
|
|
4
|
-
function isClass(obj) {
|
|
5
|
-
const isCtorClass = obj.constructor && obj.constructor.toString().substring(0, 5) === 'class';
|
|
6
|
-
if (obj.prototype === undefined) {
|
|
7
|
-
return isCtorClass;
|
|
8
|
-
}
|
|
9
|
-
const isPrototypeCtorClass = obj.prototype.constructor &&
|
|
10
|
-
obj.prototype.constructor.toString &&
|
|
11
|
-
obj.prototype.constructor.toString().substring(0, 5) === 'class';
|
|
12
|
-
return isCtorClass || isPrototypeCtorClass;
|
|
13
|
-
}
|
|
14
|
-
exports.isClass = isClass;
|
|
15
|
-
const convertOnParam = (info, paramName) => {
|
|
16
|
-
if (!info[paramName])
|
|
17
|
-
return;
|
|
18
|
-
const type = info[paramName].type;
|
|
19
|
-
if (!type)
|
|
20
|
-
return;
|
|
21
|
-
if (typeof type !== 'function')
|
|
22
|
-
return;
|
|
23
|
-
if (!isClass(type))
|
|
24
|
-
return;
|
|
25
|
-
if (!type.getModel || !type.__schemaId)
|
|
26
|
-
return;
|
|
27
|
-
info[paramName].type = type.getModel().getCleanSchema();
|
|
28
|
-
};
|
|
29
|
-
const convertTypedModel = (info) => {
|
|
30
|
-
convertOnParam(info, 'schema');
|
|
31
|
-
convertOnParam(info, 'currentSchema');
|
|
32
|
-
};
|
|
33
|
-
exports.convertTypedModel = convertTypedModel;
|
|
@@ -1,78 +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 getError_1 = __importDefault(require("./getError"));
|
|
7
|
-
const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
|
|
8
|
-
const isArray_1 = __importDefault(require("lodash/isArray"));
|
|
9
|
-
const clone_1 = __importDefault(require("lodash/clone"));
|
|
10
|
-
const isNil_1 = __importDefault(require("lodash/isNil"));
|
|
11
|
-
const difference_1 = __importDefault(require("lodash/difference"));
|
|
12
|
-
const Errors_1 = __importDefault(require("../Errors"));
|
|
13
|
-
const convertTypedModel_1 = require("./convertTypedModel");
|
|
14
|
-
async function doValidation(params) {
|
|
15
|
-
(0, convertTypedModel_1.convertTypedModel)(params);
|
|
16
|
-
const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
|
|
17
|
-
const info = { schema, doc, currentDoc, value, currentSchema, keys, options, args, addError };
|
|
18
|
-
const error = await (0, getError_1.default)(info);
|
|
19
|
-
if (error) {
|
|
20
|
-
addError(keys, error);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if ((0, isNil_1.default)(value))
|
|
24
|
-
return;
|
|
25
|
-
/**
|
|
26
|
-
* Deep validation
|
|
27
|
-
*/
|
|
28
|
-
if ((0, isPlainObject_1.default)(currentSchema.type)) {
|
|
29
|
-
const type = currentSchema.type;
|
|
30
|
-
if (type) {
|
|
31
|
-
if (type._isFieldType) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
if (typeof type.__skipChildValidation === 'function') {
|
|
35
|
-
if (await type.__skipChildValidation(value, info)) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const schemaKeys = Object.keys(currentSchema.type).filter(key => !key.startsWith('__'));
|
|
41
|
-
for (const key of schemaKeys) {
|
|
42
|
-
const itemSchema = currentSchema.type[key];
|
|
43
|
-
const itemValue = value[key];
|
|
44
|
-
const keyItemKeys = (0, clone_1.default)(keys);
|
|
45
|
-
keyItemKeys.push(key);
|
|
46
|
-
await doValidation({
|
|
47
|
-
...info,
|
|
48
|
-
currentDoc: value,
|
|
49
|
-
value: itemValue,
|
|
50
|
-
currentSchema: itemSchema,
|
|
51
|
-
keys: keyItemKeys
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
const documentKeys = Object.keys(value);
|
|
55
|
-
const notInSchemaKeys = (0, difference_1.default)(documentKeys, schemaKeys);
|
|
56
|
-
for (const key of notInSchemaKeys) {
|
|
57
|
-
const keyItemKeys = (0, clone_1.default)(keys);
|
|
58
|
-
keyItemKeys.push(key);
|
|
59
|
-
addError(keyItemKeys, Errors_1.default.NOT_IN_SCHEMA);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else if ((0, isArray_1.default)(currentSchema.type)) {
|
|
63
|
-
const itemSchema = currentSchema.type[0];
|
|
64
|
-
for (let i = 0; i < value.length; i++) {
|
|
65
|
-
const itemValue = value[i];
|
|
66
|
-
const keyItemKeys = (0, clone_1.default)(keys);
|
|
67
|
-
keyItemKeys.push(i.toString());
|
|
68
|
-
await doValidation({
|
|
69
|
-
...info,
|
|
70
|
-
currentDoc: value,
|
|
71
|
-
value: itemValue,
|
|
72
|
-
currentSchema: { type: itemSchema },
|
|
73
|
-
keys: keyItemKeys
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
exports.default = doValidation;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { SchemaMetaFieldType } from '../../types/schema';
|
|
2
|
-
import { FieldValidatorType } from '../../types/fieldValidators';
|
|
3
|
-
import { FieldType } from '../../fieldType';
|
|
4
|
-
export default function getFieldType(type: SchemaMetaFieldType | FieldValidatorType | any): FieldType;
|
|
@@ -1,13 +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 getFieldValidator_1 = __importDefault(require("./getFieldValidator"));
|
|
7
|
-
const fieldTypes_1 = __importDefault(require("../../fieldTypes"));
|
|
8
|
-
function getFieldType(type) {
|
|
9
|
-
const validatorKey = (0, getFieldValidator_1.default)(type);
|
|
10
|
-
const validator = validatorKey === 'custom' ? type : fieldTypes_1.default[validatorKey];
|
|
11
|
-
return validator;
|
|
12
|
-
}
|
|
13
|
-
exports.default = getFieldType;
|
|
@@ -1,38 +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 isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
|
|
7
|
-
const isArray_1 = __importDefault(require("lodash/isArray"));
|
|
8
|
-
const isString_1 = __importDefault(require("lodash/isString"));
|
|
9
|
-
const fieldTypes_1 = __importDefault(require("../../fieldTypes"));
|
|
10
|
-
const has_1 = __importDefault(require("lodash/has"));
|
|
11
|
-
function default_1(type) {
|
|
12
|
-
if ((0, isPlainObject_1.default)(type)) {
|
|
13
|
-
if (type._isFieldType)
|
|
14
|
-
return 'custom';
|
|
15
|
-
return 'plainObject';
|
|
16
|
-
}
|
|
17
|
-
if ((0, isArray_1.default)(type))
|
|
18
|
-
return 'array';
|
|
19
|
-
if (type === String)
|
|
20
|
-
return 'string';
|
|
21
|
-
if (type === Date)
|
|
22
|
-
return 'date';
|
|
23
|
-
if (type === Number)
|
|
24
|
-
return 'number';
|
|
25
|
-
if (type === Boolean)
|
|
26
|
-
return 'boolean';
|
|
27
|
-
if (type === 'enum')
|
|
28
|
-
return 'string';
|
|
29
|
-
if (!(0, isString_1.default)(type)) {
|
|
30
|
-
throw new Error('Field type is invalid. Pass a string or a custom field type. Got ' + type);
|
|
31
|
-
}
|
|
32
|
-
const exists = (0, has_1.default)(fieldTypes_1.default, type);
|
|
33
|
-
if (!exists) {
|
|
34
|
-
throw new Error('Field type does not exist');
|
|
35
|
-
}
|
|
36
|
-
return type;
|
|
37
|
-
}
|
|
38
|
-
exports.default = default_1;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,72 +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 getFieldValidator_1 = __importDefault(require("./getFieldValidator"));
|
|
7
|
-
const fieldType_1 = __importDefault(require("../../fieldType"));
|
|
8
|
-
const getFieldType_1 = __importDefault(require("./getFieldType"));
|
|
9
|
-
const __1 = require("../..");
|
|
10
|
-
test('returns object validator when an object is passed', async () => {
|
|
11
|
-
const validator = (0, getFieldValidator_1.default)({ name: { type: String } });
|
|
12
|
-
expect(validator).toBe('plainObject');
|
|
13
|
-
});
|
|
14
|
-
test('returns array validator when an array is passed', async () => {
|
|
15
|
-
const validator = (0, getFieldValidator_1.default)([String]);
|
|
16
|
-
expect(validator).toBe('array');
|
|
17
|
-
});
|
|
18
|
-
test('returns string validator when String is passed', async () => {
|
|
19
|
-
const validator = (0, getFieldValidator_1.default)(String);
|
|
20
|
-
expect(validator).toBe('string');
|
|
21
|
-
});
|
|
22
|
-
test('returns number validator when Number is passed', async () => {
|
|
23
|
-
const validator = (0, getFieldValidator_1.default)(Number);
|
|
24
|
-
expect(validator).toBe('number');
|
|
25
|
-
});
|
|
26
|
-
test('returns date validator when Date is passed', async () => {
|
|
27
|
-
const validator = (0, getFieldValidator_1.default)(Date);
|
|
28
|
-
expect(validator).toBe('date');
|
|
29
|
-
});
|
|
30
|
-
test('returns string validator when a string key is passed', async () => {
|
|
31
|
-
const validator = (0, getFieldValidator_1.default)('string');
|
|
32
|
-
expect(validator).toBe('string');
|
|
33
|
-
});
|
|
34
|
-
test('returns integer validator when a integer key is passed', async () => {
|
|
35
|
-
const validator = (0, getFieldValidator_1.default)('integer');
|
|
36
|
-
expect(validator).toBe('integer');
|
|
37
|
-
});
|
|
38
|
-
test('get custom field type validator', async () => {
|
|
39
|
-
const aFieldType = (0, fieldType_1.default)({
|
|
40
|
-
name: 'customFieldType'
|
|
41
|
-
});
|
|
42
|
-
const result = (0, getFieldType_1.default)(aFieldType);
|
|
43
|
-
expect(result).toBe(aFieldType);
|
|
44
|
-
});
|
|
45
|
-
test('returns unkown field validator when an unkown type function is passed', async () => {
|
|
46
|
-
expect.assertions(1);
|
|
47
|
-
try {
|
|
48
|
-
(0, getFieldValidator_1.default)(() => { });
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
expect(error.message).toMatch(/Field type is invalid/);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
test('returns unkown field validator when an unkown type string is passed', async () => {
|
|
55
|
-
expect.assertions(1);
|
|
56
|
-
try {
|
|
57
|
-
(0, getFieldValidator_1.default)('an unknown type');
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
expect(error.message).toMatch(/Field type does not exist/);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
test('passes when field type is custom (for client side)', async () => {
|
|
64
|
-
const schema = {
|
|
65
|
-
name: {
|
|
66
|
-
type: 'enum'
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
await (0, __1.clean)(schema, { name: 'test' });
|
|
70
|
-
await (0, __1.validate)(schema, { name: 'test' });
|
|
71
|
-
(0, getFieldValidator_1.default)('enum');
|
|
72
|
-
});
|