Skip to content

Commit

Permalink
Merge pull request #18 from acidiney/feat/add-support-to-array
Browse files Browse the repository at this point in the history
Feat/add support to array
  • Loading branch information
acidiney authored Oct 24, 2021
2 parents 49e875e + 4970d5a commit 6087e8d
Show file tree
Hide file tree
Showing 5 changed files with 574 additions and 141 deletions.
61 changes: 46 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,54 @@ const {
function validateLine(entry, data, index = null) {
if (entry.required && !data)
throw new ValidateException(
`Field ${entry.name} is required${index ? ` - on index #${index}` : ''}!`
`Field ${entry.name} is required${
typeof index === 'number' ? ` - on index #${index}` : ''
}!`
);

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

// Checking type
let result = null;
const strategy = {
[exceptionTypes.Enum]: (data, entry) => {
const { enumOps } = entry;

return AvailableTypes[entry.type](data, enumOps);
},
[exceptionTypes.Object]: (data, entry) => {
const { schema } = entry;
return _validate(data, schema);
},
[exceptionTypes.Array]: (data, entry) => {
let fn = strategy[entry.itemsType];

if (!fn) {
fn = AvailableTypes[entry.itemsType];
}

if (entry.type === exceptionTypes.Enum) {
const { enumOps } = entry;
result = AvailableTypes[entry.type](data, enumOps);
}
const result = validateArray(
data,
(d) => fn(d, { ...entry, type: entry.itemsType }),
entry.itemsType
);

if (entry.type === exceptionTypes.Object) {
const { schema } = entry;
result = _validate(data, schema);
}
let resultSerialized = [];

if (['Object'].includes(entry.itemsType)) {
for (const row of result) {
resultSerialized.push({})
for (const item of entry.schema) {
resultSerialized[resultSerialized.length - 1][item.serialize] = row[item.name];
}
}
}

return resultSerialized.length && resultSerialized || result;
},
};

// Checking type
let result = strategy[entry.type] && strategy[entry.type](data, entry);

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

Expand All @@ -65,7 +95,7 @@ function _validate(input, dto, index = null) {
if (!keys.includes(prop)) {
throw new ValidateException(
`Field ${prop} is required${
typeof index !== 'undefined' ? ` - on index #${index}` : ''
typeof index === 'number' ? ` - on index #${index}` : ''
}!`
);
}
Expand All @@ -75,8 +105,9 @@ function _validate(input, dto, index = null) {
for (const key of keys) {
if (entry.name === key) {
validated[entry.serialize] = validateLine(entry, input[key], index);
if(entry.defaultValue && validated[entry.serialize]===null)
validated[entry.serialize]=entry.defaultValue;

if (entry.defaultValue && validated[entry.serialize] === null)
validated[entry.serialize] = entry.defaultValue;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
validateDate,
validateObject,
validateEnum,
validateArray,
} = require('./validators/index');

module.exports = {
Expand All @@ -15,4 +16,5 @@ module.exports = {
Boolean: (value) => validateBoolean(value),
Object: (value) => validateObject(value),
Enum: (value, options) => validateEnum(value, options),
Array: (value) => validateArray(value),
};
34 changes: 19 additions & 15 deletions src/utils/validators/validateArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ const ValidateException = require('../../exceptions/ValidateException');
* @type {{validateArray: exports.validateArray}}
*/
module.exports = {
/**
*
* @param {object[]} array
* @param {string} validateFn
* @param {string} validateKey
*/
validateArray: (array, validateFn = null, validateKey = null) => {
if (!validateFn) {
if (!(array instanceof Array))
throw new ValidateException('Schema need to be an array');
return
}
/**
*
* @param {object[]} array
* @param {string} validateFn
* @param {string} validateKey
*/
validateArray: (array, validateFn = null, validateKey = null) => {
if (!(array instanceof Array))
throw new ValidateException('Schema need to be an array!');

if (!array.every((p) => validateFn(p)))
throw new ValidateException(`Schema need to be an array of ${validateKey}`);
if (validateFn) {
if (!array.every((p) => validateFn(p))) {
throw new ValidateException(
`Schema need to be an array of ${validateKey}!`
);
}
}
}

return array;
},
};
78 changes: 67 additions & 11 deletions src/utils/validators/validateSchema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const ValidateException = require('../../exceptions/ValidateException');
const AvailableTypes = require('../types')
const AvailableTypes = require('../types');
const exceptionTypes = {
Enum: 'Enum',
Array: 'Array',
Expand All @@ -9,9 +9,16 @@ const exceptionTypes = {
const validateProp = {
Enum: 'enumOps',
Object: 'schema',
Array: 'itemsType',
};

function validateExceptions(row, validateArray, validateEnum, schema, AvailableTypes) {
function validateExceptions(
row,
validateArray,
validateEnum,
schema,
AvailableTypes
) {
const prop = validateProp[row.type];

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

if (row.type === exceptionTypes.Object) {
validateSchema(row.schema, validateArray, validateEnum, schema, AvailableTypes);
validateSchema(
row.schema,
validateArray,
validateEnum,
schema,
AvailableTypes
);
}

if (row.type === exceptionTypes.Array) {

if (row.itemsType === exceptionTypes.Array) {
throw new ValidateException(
"Can't cambine type Array with itemsType Array!"
);
}

validateType(AvailableTypes, [row.itemsType]);

validateExceptions(
{
...row,
type: row.itemsType,
},
validateArray,
validateEnum,
schema,
AvailableTypes
);
}

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

function validateSchema(params, validateArray, validateEnum, schema, AvailableTypes) {
function validateSchema(
params,
validateArray,
validateEnum,
schema,
AvailableTypes
) {
validateArray(params);
validateArray(params, AvailableTypes.Object, 'Object');

Expand All @@ -43,21 +84,36 @@ function validateSchema(params, validateArray, validateEnum, schema, AvailableTy
for (const key of schema) {
validateEnum(key, keys, `Prop '${key}' is missing on schema index ${i}!`);
}

if(row.defaultValue){
result =row.type==='Enum'?AvailableTypes[row.type](row.defaultValue,row.enumOps):AvailableTypes[row.type](row.defaultValue);
if(!result){

if (row.defaultValue) {
result =
row.type === 'Enum'
? AvailableTypes[row.type](row.defaultValue, row.enumOps)
: AvailableTypes[row.type](row.defaultValue);
if (!result) {
throw new ValidateException(
`Value of the Field defaultValue is not of the type '${row.type}' on schema index ${i}!`
);
);
}
}

validateExceptions(row, validateArray, validateEnum, schema, AvailableTypes);
validateExceptions(
row,
validateArray,
validateEnum,
schema,
AvailableTypes
);
}

validateType(
AvailableTypes,
params.map((dt) => dt.type)
);
}

function validateType(AvailableTypes, types) {
const availableTypes = Object.keys(AvailableTypes);
const types = params.map((dt) => dt.type);

types.forEach((type) => {
if (!availableTypes.includes(type)) {
Expand Down
Loading

0 comments on commit 6087e8d

Please sign in to comment.