@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.
Files changed (127) hide show
  1. package/dist/index.cjs +3635 -0
  2. package/dist/index.d.ts +185 -0
  3. package/dist/index.js +3607 -0
  4. package/package.json +19 -13
  5. package/LICENSE +0 -21
  6. package/lib/Errors.d.ts +0 -21
  7. package/lib/Errors.js +0 -22
  8. package/lib/ValidationError.d.ts +0 -14
  9. package/lib/ValidationError.js +0 -45
  10. package/lib/ValidationError.test.d.ts +0 -1
  11. package/lib/ValidationError.test.js +0 -35
  12. package/lib/clean/cleanType.d.ts +0 -3
  13. package/lib/clean/cleanType.js +0 -49
  14. package/lib/clean/getObjectNode.d.ts +0 -2
  15. package/lib/clean/getObjectNode.js +0 -14
  16. package/lib/clean/index.d.ts +0 -2
  17. package/lib/clean/index.js +0 -30
  18. package/lib/clean/index.test.d.ts +0 -1
  19. package/lib/clean/index.test.js +0 -482
  20. package/lib/clean/recursiveClean.d.ts +0 -3
  21. package/lib/clean/recursiveClean.js +0 -89
  22. package/lib/cleanKey.d.ts +0 -1
  23. package/lib/cleanKey.js +0 -25
  24. package/lib/cleanKey.test.d.ts +0 -1
  25. package/lib/cleanKey.test.js +0 -115
  26. package/lib/dotGetSchema.d.ts +0 -2
  27. package/lib/dotGetSchema.js +0 -7
  28. package/lib/fieldType.d.ts +0 -17
  29. package/lib/fieldType.js +0 -28
  30. package/lib/fieldTypes/ID.d.ts +0 -2
  31. package/lib/fieldTypes/ID.js +0 -29
  32. package/lib/fieldTypes/ID.test.d.ts +0 -1
  33. package/lib/fieldTypes/ID.test.js +0 -25
  34. package/lib/fieldTypes/any.d.ts +0 -2
  35. package/lib/fieldTypes/any.js +0 -10
  36. package/lib/fieldTypes/any.test.d.ts +0 -1
  37. package/lib/fieldTypes/any.test.js +0 -13
  38. package/lib/fieldTypes/array.d.ts +0 -2
  39. package/lib/fieldTypes/array.js +0 -23
  40. package/lib/fieldTypes/array.test.d.ts +0 -1
  41. package/lib/fieldTypes/array.test.js +0 -22
  42. package/lib/fieldTypes/blackbox.d.ts +0 -2
  43. package/lib/fieldTypes/blackbox.js +0 -15
  44. package/lib/fieldTypes/blackbox.test.d.ts +0 -1
  45. package/lib/fieldTypes/blackbox.test.js +0 -16
  46. package/lib/fieldTypes/boolean.d.ts +0 -2
  47. package/lib/fieldTypes/boolean.js +0 -36
  48. package/lib/fieldTypes/boolean.test.d.ts +0 -1
  49. package/lib/fieldTypes/boolean.test.js +0 -27
  50. package/lib/fieldTypes/date.d.ts +0 -2
  51. package/lib/fieldTypes/date.js +0 -36
  52. package/lib/fieldTypes/date.test.d.ts +0 -1
  53. package/lib/fieldTypes/date.test.js +0 -37
  54. package/lib/fieldTypes/email.d.ts +0 -2
  55. package/lib/fieldTypes/email.js +0 -29
  56. package/lib/fieldTypes/email.test.d.ts +0 -1
  57. package/lib/fieldTypes/email.test.js +0 -20
  58. package/lib/fieldTypes/enum.d.ts +0 -4
  59. package/lib/fieldTypes/enum.js +0 -57
  60. package/lib/fieldTypes/enum.test.d.ts +0 -1
  61. package/lib/fieldTypes/enum.test.js +0 -41
  62. package/lib/fieldTypes/index.d.ts +0 -14
  63. package/lib/fieldTypes/index.js +0 -29
  64. package/lib/fieldTypes/integer.d.ts +0 -2
  65. package/lib/fieldTypes/integer.js +0 -17
  66. package/lib/fieldTypes/integer.test.d.ts +0 -1
  67. package/lib/fieldTypes/integer.test.js +0 -19
  68. package/lib/fieldTypes/number.d.ts +0 -2
  69. package/lib/fieldTypes/number.js +0 -32
  70. package/lib/fieldTypes/number.test.d.ts +0 -1
  71. package/lib/fieldTypes/number.test.js +0 -28
  72. package/lib/fieldTypes/plainObject.d.ts +0 -2
  73. package/lib/fieldTypes/plainObject.js +0 -29
  74. package/lib/fieldTypes/plainObject.test.d.ts +0 -1
  75. package/lib/fieldTypes/plainObject.test.js +0 -42
  76. package/lib/fieldTypes/string.d.ts +0 -2
  77. package/lib/fieldTypes/string.js +0 -47
  78. package/lib/fieldTypes/string.test.d.ts +0 -1
  79. package/lib/fieldTypes/string.test.js +0 -28
  80. package/lib/getSchemaFromTypedModel.d.ts +0 -2
  81. package/lib/getSchemaFromTypedModel.js +0 -15
  82. package/lib/getValidationErrors/convertTypedModel.d.ts +0 -3
  83. package/lib/getValidationErrors/convertTypedModel.js +0 -33
  84. package/lib/getValidationErrors/doValidation.d.ts +0 -2
  85. package/lib/getValidationErrors/doValidation.js +0 -78
  86. package/lib/getValidationErrors/getError/getFieldType.d.ts +0 -4
  87. package/lib/getValidationErrors/getError/getFieldType.js +0 -13
  88. package/lib/getValidationErrors/getError/getFieldValidator.d.ts +0 -2
  89. package/lib/getValidationErrors/getError/getFieldValidator.js +0 -38
  90. package/lib/getValidationErrors/getError/getFieldValidator.test.d.ts +0 -1
  91. package/lib/getValidationErrors/getError/getFieldValidator.test.js +0 -72
  92. package/lib/getValidationErrors/getError/index.d.ts +0 -2
  93. package/lib/getValidationErrors/getError/index.js +0 -44
  94. package/lib/getValidationErrors/getError/index.test.d.ts +0 -1
  95. package/lib/getValidationErrors/getError/index.test.js +0 -52
  96. package/lib/getValidationErrors/getValidationErrorsObject.d.ts +0 -4
  97. package/lib/getValidationErrors/getValidationErrorsObject.js +0 -17
  98. package/lib/getValidationErrors/index.d.ts +0 -2
  99. package/lib/getValidationErrors/index.js +0 -34
  100. package/lib/getValidationErrors/index.test.d.ts +0 -1
  101. package/lib/getValidationErrors/index.test.js +0 -416
  102. package/lib/index.d.ts +0 -13
  103. package/lib/index.js +0 -42
  104. package/lib/isValid.d.ts +0 -2
  105. package/lib/isValid.js +0 -11
  106. package/lib/isValid.test.d.ts +0 -1
  107. package/lib/isValid.test.js +0 -25
  108. package/lib/types/fieldValidators.d.ts +0 -2
  109. package/lib/types/fieldValidators.js +0 -2
  110. package/lib/types/index.d.ts +0 -2
  111. package/lib/types/index.js +0 -18
  112. package/lib/types/schema.d.ts +0 -118
  113. package/lib/types/schema.js +0 -3
  114. package/lib/types/types.test.d.ts +0 -1
  115. package/lib/types/types.test.js +0 -17
  116. package/lib/validate.d.ts +0 -2
  117. package/lib/validate.js +0 -14
  118. package/lib/validate.test.d.ts +0 -1
  119. package/lib/validate.test.js +0 -29
  120. package/lib/validateKey/dotGetSchema.d.ts +0 -2
  121. package/lib/validateKey/dotGetSchema.js +0 -32
  122. package/lib/validateKey/dotGetSchema.test.d.ts +0 -1
  123. package/lib/validateKey/dotGetSchema.test.js +0 -65
  124. package/lib/validateKey/index.d.ts +0 -2
  125. package/lib/validateKey/index.js +0 -31
  126. package/lib/validateKey/index.test.d.ts +0 -1
  127. package/lib/validateKey/index.test.js +0 -84
@@ -1,115 +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 cleanKey_1 = __importDefault(require("./cleanKey"));
7
- test('autoconvert value', async () => {
8
- const schema = {
9
- number: {
10
- type: Number
11
- }
12
- };
13
- const cleaned = await (0, cleanKey_1.default)(schema, 'number', '12');
14
- expect(cleaned).toBe(12);
15
- });
16
- test('deep clean fields', async () => {
17
- const tag = {
18
- name: {
19
- type: String
20
- }
21
- };
22
- const car = {
23
- brand: {
24
- type: String
25
- },
26
- tags: {
27
- type: [tag]
28
- }
29
- };
30
- const schema = {
31
- name: {
32
- type: String
33
- },
34
- car: {
35
- type: car
36
- }
37
- };
38
- expect(await (0, cleanKey_1.default)(schema, 'car.tags', { name: 12 })).toEqual([{ name: '12' }]);
39
- expect(await (0, cleanKey_1.default)(schema, 'car.brand', 'Nissan')).toBe('Nissan');
40
- expect(await (0, cleanKey_1.default)(schema, 'car.tags', 'Nice')).toEqual(['Nice']);
41
- expect(await (0, cleanKey_1.default)(schema, 'car.tags.$.name', 12)).toBe('12');
42
- expect(await (0, cleanKey_1.default)(schema, 'car.tags.100.name', 12)).toBe('12');
43
- });
44
- test('filters keys not in schema', async () => {
45
- const schema = {
46
- services: {
47
- type: 'blackbox'
48
- }
49
- };
50
- expect(await (0, cleanKey_1.default)(schema, 'person.name', 'Nicolás')).toBe(undefined);
51
- });
52
- test('dont filter keys not in schema if specified', async () => {
53
- const schema = {
54
- services: {
55
- type: 'blackbox'
56
- }
57
- };
58
- expect(await (0, cleanKey_1.default)(schema, 'person.name', 'Nicolás', { filter: false })).toBe('Nicolás');
59
- });
60
- test('clean blackbox key', async () => {
61
- const schema = {
62
- services: {
63
- type: 'blackbox'
64
- }
65
- };
66
- expect(await (0, cleanKey_1.default)(schema, 'services.password', '123456')).toBe('123456');
67
- });
68
- test('clean key with custom clean function', async () => {
69
- let calls = 0;
70
- const schema = {
71
- services: {
72
- type: {
73
- password: { type: String },
74
- __clean({ password }) {
75
- calls++;
76
- return { password: password.slice(0, 2) };
77
- }
78
- }
79
- }
80
- };
81
- expect(await (0, cleanKey_1.default)(schema, 'services', { password: '123456' })).toEqual({ password: '12' });
82
- expect(calls).toBe(1);
83
- });
84
- test('should handle $ correctly', async () => {
85
- const Email = {
86
- address: {
87
- type: String
88
- },
89
- verified: {
90
- type: Boolean
91
- }
92
- };
93
- const user = {
94
- emails: {
95
- type: [Email]
96
- }
97
- };
98
- const cleaned = await (0, cleanKey_1.default)(user, 'emails.$.verified', 'true');
99
- expect(cleaned).toEqual(true);
100
- });
101
- test('should inject doc on cleanKey', async () => {
102
- const item = { data: 20 };
103
- const user = {
104
- data: {
105
- type: String,
106
- clean(value, { doc }) {
107
- expect(doc).toEqual(item);
108
- return value;
109
- }
110
- }
111
- };
112
- const cleaned = await (0, cleanKey_1.default)(user, 'data', item.data, { forceDoc: item });
113
- expect(cleaned).toBe('20');
114
- expect.assertions(2);
115
- });
@@ -1,2 +0,0 @@
1
- import dotGetSchema from './validateKey/dotGetSchema';
2
- export default dotGetSchema;
@@ -1,7 +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 dotGetSchema_1 = __importDefault(require("./validateKey/dotGetSchema"));
7
- exports.default = dotGetSchema_1.default;
@@ -1,17 +0,0 @@
1
- import { CleanFunction, ValidateFunction } from './types/schema';
2
- export interface FieldTypeOpts {
3
- name: string;
4
- validate?: ValidateFunction;
5
- clean?: CleanFunction;
6
- toGraphQLType?: (GraphQL: any) => any;
7
- meta?: any;
8
- }
9
- export interface FieldType {
10
- name: string;
11
- validate: ValidateFunction;
12
- clean: CleanFunction;
13
- meta?: any;
14
- toGraphQLType?: (GraphQL: any) => any;
15
- _isFieldType: boolean;
16
- }
17
- export default function fieldType(opts: FieldTypeOpts): FieldType;
package/lib/fieldType.js DELETED
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- function fieldType(opts) {
4
- const { name, validate, clean, ...otherFields } = opts;
5
- const overwrittenValidate = (value, info = {}) => {
6
- if (!info.currentSchema) {
7
- info.currentSchema = {};
8
- }
9
- return validate(value, info);
10
- };
11
- const overwrittenClean = (value, info = {}) => {
12
- if (!info.options) {
13
- info.options = {};
14
- }
15
- if (clean) {
16
- return clean(value, info);
17
- }
18
- return value;
19
- };
20
- return {
21
- ...otherFields,
22
- name,
23
- validate: overwrittenValidate,
24
- clean: overwrittenClean,
25
- _isFieldType: true
26
- };
27
- }
28
- exports.default = fieldType;
@@ -1,2 +0,0 @@
1
- declare const _default: import("../fieldType").FieldType;
2
- export default _default;
@@ -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 isString_1 = __importDefault(require("lodash/isString"));
8
- const isInteger_1 = __importDefault(require("lodash/isInteger"));
9
- const Errors_1 = __importDefault(require("../Errors"));
10
- exports.default = (0, fieldType_1.default)({
11
- name: 'ID',
12
- validate(value) {
13
- if (!(0, isString_1.default)(value) && !(0, isInteger_1.default)(value))
14
- return Errors_1.default.NOT_AN_ID;
15
- },
16
- clean(value, { options }) {
17
- if (!(0, isString_1.default)(value) && !(0, isInteger_1.default)(value))
18
- return value;
19
- const { trimStrings, removeEmptyStrings } = options;
20
- value = String(value);
21
- if (trimStrings) {
22
- value = value.trim();
23
- }
24
- if (removeEmptyStrings && value === '') {
25
- return undefined;
26
- }
27
- return value;
28
- }
29
- });
@@ -1 +0,0 @@
1
- export {};
@@ -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 ID_1 = __importDefault(require("./ID"));
7
- const Errors_1 = __importDefault(require("../Errors"));
8
- test('return an error when the value is incorrect', async () => {
9
- //@ts-ignore
10
- expect(ID_1.default.validate(false)).toBe(Errors_1.default.NOT_AN_ID);
11
- //@ts-ignore
12
- expect(ID_1.default.validate(new Date())).toBe(Errors_1.default.NOT_AN_ID);
13
- //@ts-ignore
14
- expect(ID_1.default.validate(NaN)).toBe(Errors_1.default.NOT_AN_ID);
15
- //@ts-ignore
16
- expect(ID_1.default.validate(1.01)).toBe(Errors_1.default.NOT_AN_ID);
17
- //@ts-ignore
18
- expect(ID_1.default.validate(Infinity)).toBe(Errors_1.default.NOT_AN_ID);
19
- });
20
- test('return no error when the value is correct', async () => {
21
- //@ts-ignore
22
- expect(ID_1.default.clean(1234)).toBe('1234');
23
- expect(ID_1.default.validate('helloworld')).toBeFalsy();
24
- expect(ID_1.default.validate('123456')).toBeFalsy();
25
- });
@@ -1,2 +0,0 @@
1
- declare const _default: import("../fieldType").FieldType;
2
- export default _default;
@@ -1,10 +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
- exports.default = (0, fieldType_1.default)({
8
- name: 'any',
9
- validate(value) { }
10
- });
@@ -1 +0,0 @@
1
- export {};
@@ -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 any_1 = __importDefault(require("./any"));
7
- test('return no error when the value is correct', async () => {
8
- expect(any_1.default.validate({ name: null })).toBeFalsy();
9
- expect(any_1.default.validate({ name: 'Nicolás' })).toBeFalsy();
10
- expect(any_1.default.validate(1)).toBeFalsy();
11
- expect(any_1.default.validate('string')).toBeFalsy();
12
- expect(any_1.default.validate(['hello'])).toBeFalsy();
13
- });
@@ -1,2 +0,0 @@
1
- declare const _default: import("../fieldType").FieldType;
2
- export default _default;
@@ -1,23 +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 isArray_1 = __importDefault(require("lodash/isArray"));
8
- const Errors_1 = __importDefault(require("../Errors"));
9
- exports.default = (0, fieldType_1.default)({
10
- name: 'array',
11
- validate(value) {
12
- if (!(0, isArray_1.default)(value))
13
- return Errors_1.default.NOT_AN_ARRAY;
14
- },
15
- clean(value, { options }) {
16
- if (options.autoConvert) {
17
- if (!(0, isArray_1.default)(value)) {
18
- value = [value];
19
- }
20
- }
21
- return value;
22
- }
23
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,22 +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 Errors_1 = __importDefault(require("../Errors"));
8
- it('should convert a single item into and array when cleaning', async () => {
9
- const options = { autoConvert: true };
10
- expect(array_1.default.clean('a string', { options })).toEqual(['a string']);
11
- expect(array_1.default.clean({ anObject: true }, { options })).toEqual([{ anObject: true }]);
12
- expect(array_1.default.clean({ anObject: true })).toEqual({ anObject: true });
13
- });
14
- test('return an error when the value is incorrect', async () => {
15
- expect(array_1.default.validate('a string')).toBe(Errors_1.default.NOT_AN_ARRAY);
16
- expect(array_1.default.validate(new Date())).toBe(Errors_1.default.NOT_AN_ARRAY);
17
- expect(array_1.default.validate({ anObject: true })).toBe(Errors_1.default.NOT_AN_ARRAY);
18
- });
19
- test('return no error when the value is correct', async () => {
20
- expect(array_1.default.validate(['hello'])).toBeFalsy();
21
- expect(array_1.default.validate([])).toBeFalsy();
22
- });
@@ -1,2 +0,0 @@
1
- declare const _default: import("../fieldType").FieldType;
2
- export default _default;
@@ -1,15 +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
- exports.default = (0, fieldType_1.default)({
10
- name: 'blackbox',
11
- validate(value) {
12
- if (!(0, isPlainObject_1.default)(value))
13
- return Errors_1.default.NOT_AN_OBJECT;
14
- }
15
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,16 +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 blackbox_1 = __importDefault(require("./blackbox"));
7
- const Errors_1 = __importDefault(require("../Errors"));
8
- test('return an error when the value is incorrect', async () => {
9
- expect(blackbox_1.default.validate('a string')).toBe(Errors_1.default.NOT_AN_OBJECT);
10
- expect(blackbox_1.default.validate(new Date())).toBe(Errors_1.default.NOT_AN_OBJECT);
11
- expect(blackbox_1.default.validate([])).toBe(Errors_1.default.NOT_AN_OBJECT);
12
- });
13
- test('return no error when the value is correct', async () => {
14
- expect(blackbox_1.default.validate({ name: null })).toBeFalsy();
15
- expect(blackbox_1.default.validate({ name: 'Nicolás' })).toBeFalsy();
16
- });
@@ -1,2 +0,0 @@
1
- declare const _default: import("../fieldType").FieldType;
2
- export default _default;
@@ -1,36 +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 isBoolean_1 = __importDefault(require("lodash/isBoolean"));
8
- const Errors_1 = __importDefault(require("../Errors"));
9
- exports.default = (0, fieldType_1.default)({
10
- name: 'boolean',
11
- validate(value) {
12
- if (!(0, isBoolean_1.default)(value))
13
- return Errors_1.default.NOT_A_BOOLEAN;
14
- },
15
- clean(value, { options }) {
16
- if (options.autoConvert) {
17
- if (typeof value === 'string') {
18
- if (value === 'true') {
19
- value = true;
20
- }
21
- if (value === 'false') {
22
- value = false;
23
- }
24
- }
25
- if (typeof value === 'number') {
26
- if (value === 0) {
27
- value = false;
28
- }
29
- else {
30
- value = true;
31
- }
32
- }
33
- }
34
- return value;
35
- }
36
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,27 +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 boolean_1 = __importDefault(require("./boolean"));
7
- const Errors_1 = __importDefault(require("../Errors"));
8
- test('return an error when the value is incorrect', async () => {
9
- expect(boolean_1.default.validate(['Hello'])).toBe(Errors_1.default.NOT_A_BOOLEAN);
10
- expect(boolean_1.default.validate({ name: 'Nicolás' })).toBe(Errors_1.default.NOT_A_BOOLEAN);
11
- expect(boolean_1.default.validate(new Date())).toBe(Errors_1.default.NOT_A_BOOLEAN);
12
- expect(boolean_1.default.validate(1)).toBe(Errors_1.default.NOT_A_BOOLEAN);
13
- expect(boolean_1.default.validate(true)).toBeFalsy();
14
- expect(boolean_1.default.validate(false)).toBeFalsy();
15
- });
16
- test('autoconvert', async () => {
17
- const info = { options: { autoConvert: true } };
18
- expect(boolean_1.default.clean('hello', info)).toBe('hello');
19
- expect(boolean_1.default.clean('true', info)).toBe(true);
20
- expect(boolean_1.default.clean('false', info)).toBe(false);
21
- expect(boolean_1.default.clean(1, info)).toBe(true);
22
- expect(boolean_1.default.clean(0, info)).toBe(false);
23
- expect(boolean_1.default.clean(true, info)).toBe(true);
24
- expect(boolean_1.default.clean(false, info)).toBe(false);
25
- // dont autoconvert
26
- expect(boolean_1.default.clean('false')).toBe('false');
27
- });
@@ -1,2 +0,0 @@
1
- declare const _default: import("../fieldType").FieldType;
2
- export default _default;
@@ -1,36 +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 isDate_1 = __importDefault(require("lodash/isDate"));
8
- const Errors_1 = __importDefault(require("../Errors"));
9
- const isString_1 = __importDefault(require("lodash/isString"));
10
- const isNumber_1 = __importDefault(require("lodash/isNumber"));
11
- exports.default = (0, fieldType_1.default)({
12
- name: 'date',
13
- validate(value) {
14
- if (!(0, isDate_1.default)(value))
15
- return Errors_1.default.NOT_A_DATE;
16
- },
17
- clean(value, { options }) {
18
- if (options.autoConvert) {
19
- if ((0, isString_1.default)(value)) {
20
- const result = new Date(value);
21
- if (isNaN(result.getTime())) {
22
- return value;
23
- }
24
- value = result;
25
- }
26
- else if ((0, isNumber_1.default)(value)) {
27
- const result = new Date(value);
28
- if (isNaN(result.getTime())) {
29
- return value;
30
- }
31
- value = result;
32
- }
33
- }
34
- return value;
35
- }
36
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,37 +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 date_1 = __importDefault(require("./date"));
7
- const Errors_1 = __importDefault(require("../Errors"));
8
- test('return an error when the value is incorrect', async () => {
9
- expect(date_1.default.validate(['Hello'])).toBe(Errors_1.default.NOT_A_DATE);
10
- expect(date_1.default.validate({ name: 'Nicolás' })).toBe(Errors_1.default.NOT_A_DATE);
11
- expect(date_1.default.validate('A real string')).toBe(Errors_1.default.NOT_A_DATE);
12
- expect(date_1.default.validate(1234)).toBe(Errors_1.default.NOT_A_DATE);
13
- });
14
- test('return no error when the value is correct', async () => {
15
- expect(date_1.default.validate(new Date())).toBeFalsy();
16
- });
17
- test('dont autoConvert if not specified', async () => {
18
- const info = { options: { autoConvert: false } };
19
- expect(date_1.default.clean('2018-07-21T16:00:00.000Z', info)).toBe('2018-07-21T16:00:00.000Z');
20
- });
21
- test('return the same type if invalid is passed', async () => {
22
- const info = { options: { autoConvert: true } };
23
- expect(date_1.default.clean([123], info)).toEqual([123]);
24
- });
25
- test('clean string types', async () => {
26
- const info = { options: { autoConvert: true } };
27
- expect(date_1.default.clean('2018-07-21T16:00:00.000Z', info)).toEqual(new Date('2018-07-21T16:00:00.000Z'));
28
- });
29
- test('clean number types', async () => {
30
- const info = { options: { autoConvert: true } };
31
- expect(date_1.default.clean(1532188800000, info)).toEqual(new Date('2018-07-21T16:00:00.000Z'));
32
- });
33
- test('return the same value when invalid types are passed', async () => {
34
- const info = { options: { autoConvert: true } };
35
- expect(date_1.default.clean('Nicolás', info)).toBe('Nicolás');
36
- expect(date_1.default.clean(1532188800000000000, info)).toBe(1532188800000000000);
37
- });
@@ -1,2 +0,0 @@
1
- declare const _default: import("../fieldType").FieldType;
2
- export default _default;
@@ -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 isString_1 = __importDefault(require("lodash/isString"));
8
- const Errors_1 = __importDefault(require("../Errors"));
9
- const isNil_1 = __importDefault(require("lodash/isNil"));
10
- exports.default = (0, fieldType_1.default)({
11
- name: 'email',
12
- validate(value, { currentSchema }) {
13
- if ((value === '' || (0, isNil_1.default)(value)) && !currentSchema.optional) {
14
- return Errors_1.default.REQUIRED;
15
- }
16
- if (value && !(0, isString_1.default)(value))
17
- return Errors_1.default.NOT_A_STRING;
18
- // eslint-disable-next-line
19
- const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
20
- if (value && !regex.test(value))
21
- return Errors_1.default.NOT_AN_EMAIL;
22
- },
23
- clean(value) {
24
- if (typeof value === 'string') {
25
- value = value.toLowerCase();
26
- }
27
- return value;
28
- }
29
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,20 +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 email_1 = __importDefault(require("./email"));
7
- const Errors_1 = __importDefault(require("../Errors"));
8
- test('return an error when the value is incorrect', async () => {
9
- expect(email_1.default.validate(['Hello'])).toBe(Errors_1.default.NOT_A_STRING);
10
- expect(email_1.default.validate({ name: 'Nicolás' })).toBe(Errors_1.default.NOT_A_STRING);
11
- expect(email_1.default.validate(new Date())).toBe(Errors_1.default.NOT_A_STRING);
12
- expect(email_1.default.validate('astring')).toBe(Errors_1.default.NOT_AN_EMAIL);
13
- expect(email_1.default.validate(null)).toBe(Errors_1.default.REQUIRED);
14
- expect(email_1.default.validate('')).toBe(Errors_1.default.REQUIRED);
15
- expect(email_1.default.validate('', { currentSchema: { optional: true } })).toBeFalsy();
16
- expect(email_1.default.validate(null, { currentSchema: { optional: true } })).toBeFalsy();
17
- });
18
- test('return no error when the value is correct', async () => {
19
- expect(email_1.default.validate('aemail@email.com')).toBeFalsy();
20
- });
@@ -1,4 +0,0 @@
1
- import { FieldType } from '../fieldType';
2
- export default function createEnum<TValues extends readonly string[]>(name: string, values: TValues): FieldType & {
3
- type: TValues[number];
4
- };
@@ -1,57 +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
- function createEnum(name, values) {
11
- return {
12
- type: values[0],
13
- ...(0, fieldType_1.default)({
14
- name: 'enum',
15
- meta: {
16
- enumName: name,
17
- enumValues: values
18
- },
19
- toGraphQLType: GraphQL => {
20
- global.GraphQLEnums = global.GraphQLEnums || {};
21
- global.GraphQLEnums[name] =
22
- global.GraphQLEnums[name] ||
23
- new GraphQL.GraphQLEnumType({
24
- name,
25
- values: values.reduce((result, value) => {
26
- result[value] = { value };
27
- return result;
28
- }, {})
29
- });
30
- return global.GraphQLEnums[name];
31
- },
32
- validate(value, { currentSchema }) {
33
- if (!(0, isString_1.default)(value))
34
- return Errors_1.default.NOT_A_STRING;
35
- if (!(0, includes_1.default)(values, value)) {
36
- return Errors_1.default.NOT_AN_ALLOWED_VALUE;
37
- }
38
- if (value === '' && !currentSchema.optional) {
39
- return Errors_1.default.REQUIRED;
40
- }
41
- },
42
- clean(value, { options: { autoConvert, trimStrings, removeEmptyStrings } }) {
43
- if (autoConvert) {
44
- value = String(value);
45
- }
46
- if (trimStrings) {
47
- value = value.trim();
48
- }
49
- if (removeEmptyStrings && value === '') {
50
- return undefined;
51
- }
52
- return value;
53
- }
54
- })
55
- };
56
- }
57
- exports.default = createEnum;
@@ -1 +0,0 @@
1
- export {};