Skip to content

Commit

Permalink
move files around for better structure
Browse files Browse the repository at this point in the history
  • Loading branch information
phixid committed Jul 22, 2020
1 parent 560badd commit c28a232
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/__mocks__/address-mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Primitives } from '../utils/validation/ModelValidator';
import { Primitives } from '../lib/validation/model-validator';

export const mockAddressModel = [
{ key: 'street', required: true, type: Primitives.String },
Expand Down
2 changes: 1 addition & 1 deletion src/__mocks__/user-mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mockAddress, mockAddressModel } from './address-mock';
import { NonPrimitives, Primitives } from '../utils/validation/ModelValidator';
import { NonPrimitives, Primitives } from '../lib/validation/model-validator';

export const mockUserModel = [
{ key: 'firstname', required: true, type: Primitives.String },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BooleanValidator } from './BooleanValidator';
import { BooleanValidator } from './boolean-validator';

describe('BooleanValidator', () => {
const validator = new BooleanValidator();
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions src/lib/validation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Validator {
validate(value: any): boolean;
}

export { BooleanValidator } from './boolean-validator';
export { ModelValidator } from './model-validator';
export { NumberValidator } from './number-validator';
export { ObjectValidator } from './object-validator';
export { StringValidator } from './string-validator';
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('ModelValidator', () => {
const invalidUserWithAddressValidator = new ModelValidator(invalidMockUserWithAddressModel, validators);
const { errors, isValid } = invalidUserWithAddressValidator.validate(mockUserWithAddress);

expect(errors).toEqual(['Model validation error: missing nested model address']);
expect(errors).toEqual(['Model validation error: missing nested model for key address']);
expect(isValid).toEqual(false);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,11 @@ export class ModelValidator {
const nestedModels = this.model.filter((property) => property.type === NonPrimitives.Object);
nestedModels.forEach((nestedModel) => {
const { key, model } = nestedModel;

if (model && key) {
const nestedObj = obj[key];
const validator = new ModelValidator(model, this.typeValidators);
const { errors } = validator.validate(nestedObj);
errors.forEach(this.invalidate);
} else {
this.invalidate(`missing nested model ${key}`);
}
if (!model) return this.invalidate(`missing nested model for key ${key}`);
const nestedObj = obj[key];
const validator = new ModelValidator(model, this.typeValidators);
const { errors } = validator.validate(nestedObj);
errors.forEach(this.invalidate);
});
};

Expand All @@ -80,13 +76,9 @@ export class ModelValidator {
const typeValidator = this.getValidatorForType(expectedType);
const objectProperty = obj[key];

if (typeValidator) {
if (this.hasInvalidType(objectProperty, typeValidator)) {
const errorMsg = `expected ${key} to have type ${expectedType} but has type ${typeof objectProperty}`;
return this.invalidate(errorMsg);
}
} else {
return this.invalidate(`missing validator for type ${expectedType}`);
if (!typeValidator) return this.invalidate(`missing validator for type ${expectedType}`);
if (this.hasInvalidType(objectProperty, typeValidator)) {
this.invalidate(`expected ${key} to have type ${expectedType} but has type ${typeof objectProperty}`);
}
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NumberValidator } from './NumberValidator';
import { NumberValidator } from './number-validator';

describe('NumberValidator', () => {
const validator = new NumberValidator();
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjectValidator } from './ObjectValidator';
import { ObjectValidator } from './object-validator';

describe('ObjectValidator', () => {
const validator = new ObjectValidator();
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StringValidator } from './StringValidator';
import { StringValidator } from './string-validator';

describe('StringValidator', () => {
const validator = new StringValidator();
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions src/utils/validation/Validator.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/utils/validation/index.ts

This file was deleted.

0 comments on commit c28a232

Please sign in to comment.