Skip to content

Commit 6087e8d

Browse files
authored
Merge pull request #18 from acidiney/feat/add-support-to-array
Feat/add support to array
2 parents 49e875e + 4970d5a commit 6087e8d

File tree

5 files changed

+574
-141
lines changed

5 files changed

+574
-141
lines changed

src/index.js

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,54 @@ const {
2020
function validateLine(entry, data, index = null) {
2121
if (entry.required && !data)
2222
throw new ValidateException(
23-
`Field ${entry.name} is required${index ? ` - on index #${index}` : ''}!`
23+
`Field ${entry.name} is required${
24+
typeof index === 'number' ? ` - on index #${index}` : ''
25+
}!`
2426
);
2527

2628
// Ignore type checking if doesn't have data and not required
2729
if (!entry.required && !data) return null;
2830

29-
// Checking type
30-
let result = null;
31+
const strategy = {
32+
[exceptionTypes.Enum]: (data, entry) => {
33+
const { enumOps } = entry;
34+
35+
return AvailableTypes[entry.type](data, enumOps);
36+
},
37+
[exceptionTypes.Object]: (data, entry) => {
38+
const { schema } = entry;
39+
return _validate(data, schema);
40+
},
41+
[exceptionTypes.Array]: (data, entry) => {
42+
let fn = strategy[entry.itemsType];
43+
44+
if (!fn) {
45+
fn = AvailableTypes[entry.itemsType];
46+
}
3147

32-
if (entry.type === exceptionTypes.Enum) {
33-
const { enumOps } = entry;
34-
result = AvailableTypes[entry.type](data, enumOps);
35-
}
48+
const result = validateArray(
49+
data,
50+
(d) => fn(d, { ...entry, type: entry.itemsType }),
51+
entry.itemsType
52+
);
3653

37-
if (entry.type === exceptionTypes.Object) {
38-
const { schema } = entry;
39-
result = _validate(data, schema);
40-
}
54+
let resultSerialized = [];
55+
56+
if (['Object'].includes(entry.itemsType)) {
57+
for (const row of result) {
58+
resultSerialized.push({})
59+
for (const item of entry.schema) {
60+
resultSerialized[resultSerialized.length - 1][item.serialize] = row[item.name];
61+
}
62+
}
63+
}
64+
65+
return resultSerialized.length && resultSerialized || result;
66+
},
67+
};
68+
69+
// Checking type
70+
let result = strategy[entry.type] && strategy[entry.type](data, entry);
4171

4272
if (!Object.keys(exceptionTypes).includes(entry.type)) {
4373
result = AvailableTypes[entry.type](data);
@@ -46,7 +76,7 @@ function validateLine(entry, data, index = null) {
4676
if (data && !result)
4777
throw new ValidateException(
4878
`Field ${entry.name} with value ${data}, is not typeof ${entry.type}${
49-
typeof index !== 'undefined' ? ` - on index #${index}` : ''
79+
typeof index === 'number' ? ` - on index #${index}` : ''
5080
}!`
5181
);
5282

@@ -65,7 +95,7 @@ function _validate(input, dto, index = null) {
6595
if (!keys.includes(prop)) {
6696
throw new ValidateException(
6797
`Field ${prop} is required${
68-
typeof index !== 'undefined' ? ` - on index #${index}` : ''
98+
typeof index === 'number' ? ` - on index #${index}` : ''
6999
}!`
70100
);
71101
}
@@ -75,8 +105,9 @@ function _validate(input, dto, index = null) {
75105
for (const key of keys) {
76106
if (entry.name === key) {
77107
validated[entry.serialize] = validateLine(entry, input[key], index);
78-
if(entry.defaultValue && validated[entry.serialize]===null)
79-
validated[entry.serialize]=entry.defaultValue;
108+
109+
if (entry.defaultValue && validated[entry.serialize] === null)
110+
validated[entry.serialize] = entry.defaultValue;
80111
}
81112
}
82113
}

src/utils/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
validateDate,
77
validateObject,
88
validateEnum,
9+
validateArray,
910
} = require('./validators/index');
1011

1112
module.exports = {
@@ -15,4 +16,5 @@ module.exports = {
1516
Boolean: (value) => validateBoolean(value),
1617
Object: (value) => validateObject(value),
1718
Enum: (value, options) => validateEnum(value, options),
19+
Array: (value) => validateArray(value),
1820
};

src/utils/validators/validateArray.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@ const ValidateException = require('../../exceptions/ValidateException');
77
* @type {{validateArray: exports.validateArray}}
88
*/
99
module.exports = {
10-
/**
11-
*
12-
* @param {object[]} array
13-
* @param {string} validateFn
14-
* @param {string} validateKey
15-
*/
16-
validateArray: (array, validateFn = null, validateKey = null) => {
17-
if (!validateFn) {
18-
if (!(array instanceof Array))
19-
throw new ValidateException('Schema need to be an array');
20-
return
21-
}
10+
/**
11+
*
12+
* @param {object[]} array
13+
* @param {string} validateFn
14+
* @param {string} validateKey
15+
*/
16+
validateArray: (array, validateFn = null, validateKey = null) => {
17+
if (!(array instanceof Array))
18+
throw new ValidateException('Schema need to be an array!');
2219

23-
if (!array.every((p) => validateFn(p)))
24-
throw new ValidateException(`Schema need to be an array of ${validateKey}`);
20+
if (validateFn) {
21+
if (!array.every((p) => validateFn(p))) {
22+
throw new ValidateException(
23+
`Schema need to be an array of ${validateKey}!`
24+
);
25+
}
2526
}
26-
}
27+
28+
return array;
29+
},
30+
};

src/utils/validators/validateSchema.js

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const ValidateException = require('../../exceptions/ValidateException');
2-
const AvailableTypes = require('../types')
2+
const AvailableTypes = require('../types');
33
const exceptionTypes = {
44
Enum: 'Enum',
55
Array: 'Array',
@@ -9,9 +9,16 @@ const exceptionTypes = {
99
const validateProp = {
1010
Enum: 'enumOps',
1111
Object: 'schema',
12+
Array: 'itemsType',
1213
};
1314

14-
function validateExceptions(row, validateArray, validateEnum, schema, AvailableTypes) {
15+
function validateExceptions(
16+
row,
17+
validateArray,
18+
validateEnum,
19+
schema,
20+
AvailableTypes
21+
) {
1522
const prop = validateProp[row.type];
1623

1724
if (prop) {
@@ -21,7 +28,35 @@ function validateExceptions(row, validateArray, validateEnum, schema, AvailableT
2128
);
2229

2330
if (row.type === exceptionTypes.Object) {
24-
validateSchema(row.schema, validateArray, validateEnum, schema, AvailableTypes);
31+
validateSchema(
32+
row.schema,
33+
validateArray,
34+
validateEnum,
35+
schema,
36+
AvailableTypes
37+
);
38+
}
39+
40+
if (row.type === exceptionTypes.Array) {
41+
42+
if (row.itemsType === exceptionTypes.Array) {
43+
throw new ValidateException(
44+
"Can't cambine type Array with itemsType Array!"
45+
);
46+
}
47+
48+
validateType(AvailableTypes, [row.itemsType]);
49+
50+
validateExceptions(
51+
{
52+
...row,
53+
type: row.itemsType,
54+
},
55+
validateArray,
56+
validateEnum,
57+
schema,
58+
AvailableTypes
59+
);
2560
}
2661

2762
if (row.type === exceptionTypes.Enum) {
@@ -32,7 +67,13 @@ function validateExceptions(row, validateArray, validateEnum, schema, AvailableT
3267
return;
3368
}
3469

35-
function validateSchema(params, validateArray, validateEnum, schema, AvailableTypes) {
70+
function validateSchema(
71+
params,
72+
validateArray,
73+
validateEnum,
74+
schema,
75+
AvailableTypes
76+
) {
3677
validateArray(params);
3778
validateArray(params, AvailableTypes.Object, 'Object');
3879

@@ -43,21 +84,36 @@ function validateSchema(params, validateArray, validateEnum, schema, AvailableTy
4384
for (const key of schema) {
4485
validateEnum(key, keys, `Prop '${key}' is missing on schema index ${i}!`);
4586
}
46-
47-
if(row.defaultValue){
48-
result =row.type==='Enum'?AvailableTypes[row.type](row.defaultValue,row.enumOps):AvailableTypes[row.type](row.defaultValue);
49-
if(!result){
87+
88+
if (row.defaultValue) {
89+
result =
90+
row.type === 'Enum'
91+
? AvailableTypes[row.type](row.defaultValue, row.enumOps)
92+
: AvailableTypes[row.type](row.defaultValue);
93+
if (!result) {
5094
throw new ValidateException(
5195
`Value of the Field defaultValue is not of the type '${row.type}' on schema index ${i}!`
52-
);
96+
);
5397
}
5498
}
5599

56-
validateExceptions(row, validateArray, validateEnum, schema, AvailableTypes);
100+
validateExceptions(
101+
row,
102+
validateArray,
103+
validateEnum,
104+
schema,
105+
AvailableTypes
106+
);
57107
}
58108

109+
validateType(
110+
AvailableTypes,
111+
params.map((dt) => dt.type)
112+
);
113+
}
114+
115+
function validateType(AvailableTypes, types) {
59116
const availableTypes = Object.keys(AvailableTypes);
60-
const types = params.map((dt) => dt.type);
61117

62118
types.forEach((type) => {
63119
if (!availableTypes.includes(type)) {

0 commit comments

Comments
 (0)