Skip to content

Commit

Permalink
Fix Shared validator in case of undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
akibrk committed Sep 11, 2023
1 parent 95dbcbb commit 6098454
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
7 changes: 3 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"cSpell.words": [
"arvyo"
]
}
"cSpell.words": ["arvyo"],
"workbench.colorCustomizations": {}
}
2 changes: 1 addition & 1 deletion src/spec/classValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Class Validator', () => {

it('Checks missing properties', () => {
expect(loginValidator.isValid({})).toBeFalsy();
expect(loginValidator.isValid({ email: 'akib' })).toBeFalsy();
expect(loginValidator.isValid({ email: 'akib' })).toBeTruthy();
});

it('Valid login model', () => {
Expand Down
64 changes: 34 additions & 30 deletions src/validator/Shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import { Type } from '../types';

/**
* Validates single property against a validation rule
* @param obj to validate
* @param item to validate
* @param propName name of the property
* @param rule Validation Rule
* @returns ValidationError | undefined
*/
export function validateRule(obj: any, propName: string, rule: ValidationRule): ValidationError | undefined {
export function validateRule(item: any, propName: string, rule: ValidationRule): ValidationError | undefined {
let vError: ValidationError = new VError(propName);

// If there isn't a rule defined for the property ignore it
if (!rule || rule.allowNull) {
return undefined;
}
if (!rule) return undefined;

// If null is allowed and the value is null ignore
if(rule.allowNull && item === null) return undefined;

// If note required and the value is undefined ignore
if(!rule.required && item === undefined) return undefined;

if (rule.required) {
if (!obj || typeof obj === 'undefined') {
if (!item || typeof item === 'undefined') {
vError.errors.push({
message: `Field is required, missing field: ${propName}`,
});
Expand All @@ -28,27 +32,27 @@ export function validateRule(obj: any, propName: string, rule: ValidationRule):

switch (rule.type) {
case Type.email:
if (typeof obj !== Type.string) {
if (typeof item !== Type.string) {
vError.errors.push({
message: `Invalid type, expected ${rule.type} got ${typeof obj}`,
message: `Invalid type, expected ${rule.type} got ${typeof item}`,
});
} else {
const { value, error } = isEmailAddress(obj);
const { value, error } = isEmailAddress(item);
if (!value && error) {
vError.errors.push({
message: error,
});
}

if (rule.minLength) {
if (obj.length < rule.minLength) {
if (item.length < rule.minLength) {
vError.errors.push({
message: `Invalid ${rule.type} address, must be equal or longer than ${rule.minLength}`,
});
}
}
if (rule.maxLength) {
if (obj.length > rule.maxLength) {
if (item.length > rule.maxLength) {
vError.errors.push({
message: `Invalid ${rule.type} address, must be equal or shorter than ${rule.maxLength}`,
});
Expand All @@ -57,27 +61,27 @@ export function validateRule(obj: any, propName: string, rule: ValidationRule):
}
break;
case Type.mobile:
if (typeof obj !== Type.string) {
if (typeof item !== Type.string) {
vError.errors.push({
message: `Invalid type, expected ${rule.type} got ${typeof obj}`,
message: `Invalid type, expected ${rule.type} got ${typeof item}`,
});
} else {
const { value, error } = isMobileNumber(obj);
const { value, error } = isMobileNumber(item);
if (!value && error) {
vError.errors.push({
message: error,
});
}

if (rule.minLength) {
if (obj.length < rule.minLength) {
if (item.length < rule.minLength) {
vError.errors.push({
message: `Invalid ${rule.type} number, must be equal or longer than ${rule.minLength}`,
});
}
}
if (rule.maxLength) {
if (obj.length > rule.maxLength) {
if (item.length > rule.maxLength) {
vError.errors.push({
message: `Invalid ${rule.type} number, must be equal or shorter than ${rule.maxLength}`,
});
Expand All @@ -86,78 +90,78 @@ export function validateRule(obj: any, propName: string, rule: ValidationRule):
}
break;
case Type.array:
if (typeof obj !== 'object') {
if (typeof item !== 'object') {
vError.errors.push({
message: `Invalid type, expected ${rule.type} got ${obj === null ? 'null' : typeof obj}`,
message: `Invalid type, expected ${rule.type} got ${item === null ? 'null' : typeof item}`,
});
}
if (rule.minLength) {
if (obj.length < rule.minLength) {
if (item.length < rule.minLength) {
vError.errors.push({
message: `Invalid '${propName}', must be equal or longer than ${rule.minLength}`,
});
}
}

if (rule.maxLength) {
if (obj.length > rule.maxLength) {
if (item.length > rule.maxLength) {
vError.errors.push({
message: `Invalid '${propName}', must be equal or shorter than ${rule.maxLength}`,
});
}
}
break;
case Type.string:
if (rule.type !== typeof obj) {
if (rule.type !== typeof item) {
vError.errors.push({
message: `Invalid type, expected ${rule.type} got ${obj === null ? 'null' : typeof obj}`,
message: `Invalid type, expected ${rule.type} got ${item === null ? 'null' : typeof item}`,
});
break;
}

if (rule.minLength) {
if (obj.length < rule.minLength) {
if (item.length < rule.minLength) {
vError.errors.push({
message: `Invalid '${propName}', must be equal or longer than ${rule.minLength}`,
});
}
}

if (rule.maxLength) {
if (obj.length > rule.maxLength) {
if (item.length > rule.maxLength) {
vError.errors.push({
message: `Invalid '${propName}', must be equal or shorter than ${rule.maxLength}`,
});
}
}
break;
case Type.number:
if (rule.type !== typeof obj) {
if (rule.type !== typeof item) {
vError.errors.push({
message: `Invalid type, expected ${rule.type} got ${obj === null ? 'null' : typeof obj}`,
message: `Invalid type, expected ${rule.type} got ${item === null ? 'null' : typeof item}`,
});
break;
}
if (rule.min) {
if (obj < rule.min) {
if (item < rule.min) {
vError.errors.push({
message: `Invalid '${propName}', must be equal or larger than ${rule.min}`,
});
}
}

if (rule.max) {
if (obj > rule.max) {
if (item > rule.max) {
vError.errors.push({
message: `Invalid '${propName}', must be equal or less than ${rule.max}`,
});
}
}
break;
default:
if (rule.type !== typeof obj) {
if (rule.type !== typeof item) {
vError.errors.push({
message: `Invalid type, expected ${rule.type} got ${obj === null ? 'null' : typeof obj}`,
message: `Invalid type, expected ${rule.type} got ${item === null ? 'null' : typeof item}`,
});
}
}
Expand Down

0 comments on commit 6098454

Please sign in to comment.