diff --git a/client/core/src/directives/specs/validation.spec.js b/client/core/src/directives/specs/validation.spec.js index 1b39a862c..5a819aced 100644 --- a/client/core/src/directives/specs/validation.spec.js +++ b/client/core/src/directives/specs/validation.spec.js @@ -26,7 +26,7 @@ describe('Directive validation', () => { }).toThrow(new Error(`vnValidation: Attribute must have this syntax: [entity].[field]`)); }); - it('should throw an error if the window.validations are not defined', () => { + it('should throw an error if the window.validations aint defined', () => { let html = ``; expect(() => { @@ -34,15 +34,12 @@ describe('Directive validation', () => { }).toThrow(new Error(`vnValidation: Entity 'User' doesn't exist`)); }); - describe('Validator Presence()', () => { + describe('Validator presence()', () => { it('should not validate the user name as it is an empty string', () => { let html = `
`; let validations = {User: {validations: {name: [{validation: 'presence'}]}}}; compile(html, validations, 'Spiderman'); scope.user.name = ''; - - expect(element[0].classList).toContain('ng-valid'); - expect(element[0].classList).not.toContain('ng-invalid'); scope.$digest(); expect(element[0].classList).toContain('ng-invalid'); @@ -50,25 +47,134 @@ describe('Directive validation', () => { }); }); - it(`should not validate the name as it's format is incorrect`, () => { - let html = ``; - let validations = {User: {validations: {name: [{validation: 'presence'}]}}}; - compile(html, validations, 'spiderman'); - scope.user.name = ''; - scope.$digest(); + describe('Validator absence()', () => { + it('should not validate the entity as it should be an empty string', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'absence'}]}}}; + compile(html, validations, 'Spiderman'); + scope.$digest(); - expect(element[0].classList).toContain('ng-invalid'); - expect(element[0].classList).not.toContain('ng-valid'); + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + + it('should validate the entity as it is an empty string', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'absence'}]}}}; + compile(html, validations, ''); + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); }); - it(`should validate the email as it's format is correct`, () => { - let html = ``; - let validations = {User: {validations: {email: [{validation: 'presence'}]}}}; - compile(html, validations, 'user@verdnatura.es'); - scope.user.email = 'user@verdnatura.es'; - scope.$digest(); + describe('Validator length()', () => { + it('should not validate the user name as it should have min length of 15', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', min: 10, max: 50, is: 15}]}}}; + compile(html, validations, 'fifteen!'); + scope.$digest(); - expect(element[0].classList).toContain('ng-valid'); - expect(element[0].classList).not.toContain('ng-invalid'); + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + + it('should validate the user name as it has length of 15', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', min: 10, max: 50, is: 15}]}}}; + compile(html, validations, 'fifteen length!'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); + + it('should not validate the user name as it should have min length of 10', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', min: 10}]}}}; + compile(html, validations, 'shortname'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + + it('should validate the user name as its length is greater then the minimum', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', min: 10}]}}}; + compile(html, validations, 'verylongname'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); + + it('should not validate the user name as its length is greater then the maximum', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', max: 10}]}}}; + compile(html, validations, 'toolongname'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); }); + + describe('Validator numericality()', () => { + it('should not validate the phone number as it should a integer', () => { + let html = ``; + let validations = {User: {validations: {phone: [{validation: 'numericality', is: 'what is this?'}]}}}; + compile(html, validations, 'spiderman'); + scope.user.phone = 'this is not a phone number!'; + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + + it('should validate the phone number as it an integer', () => { + let html = ``; + let validations = {User: {validations: {phone: [{validation: 'numericality', is: 'what is this?'}]}}}; + compile(html, validations, 'spiderman'); + scope.user.phone = '555555555'; + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); + }); + + // it(`should not validate the name as it's format is incorrect`, () => { + // let html = ``; + // let validations = {User: {validations: {name: [{validation: 'presence'}]}}}; + // compile(html, validations, 'spiderman'); + // scope.user.name = ''; + // scope.$digest(); + + // expect(element[0].classList).toContain('ng-invalid'); + // expect(element[0].classList).not.toContain('ng-valid'); + // }); + + // it(`should validate the email as it's format is correct`, () => { + // let html = ``; + // let validations = {User: {validations: {email: [{validation: 'presence'}]}}}; + // compile(html, validations, 'spiderman'); + // scope.user.email = 'user@verdnatura.es'; + // scope.$digest(); + + // expect(element[0].classList).toContain('ng-valid'); + // expect(element[0].classList).not.toContain('ng-invalid'); + // }); + + // it(`should not validate the email as it's missing the @`, () => { + // let html = ``; + // let validations = {User: {validations: {email: [{validation: 'presence'}]}}}; + // compile(html, validations, 'spiderman'); + // scope.user.email = 'userverdnatura.es'; + // scope.$digest(); + + // expect(element[0].classList).toContain('ng-invalid'); + // expect(element[0].classList).not.toContain('ng-valid'); + // }); }); diff --git a/client/core/src/directives/validation.js b/client/core/src/directives/validation.js index db34fe12b..10669e738 100644 --- a/client/core/src/directives/validation.js +++ b/client/core/src/directives/validation.js @@ -40,7 +40,7 @@ export function directive(interpolate, compile, $window) { let errorShown = false; input.$options.$$options.allowInvalid = true; - input.$validators.entity = function(value) { + input.$validators.entity = value => { try { validateAll(value, validations); return true; diff --git a/client/core/src/lib/validator.js b/client/core/src/lib/validator.js index d25776ab3..018d1b26e 100644 --- a/client/core/src/lib/validator.js +++ b/client/core/src/lib/validator.js @@ -9,11 +9,12 @@ export const validators = { if (!validator.isEmpty(value)) throw new Error(`Value should be empty`); }, - length: function(value, conf) { + length: (value, conf) => { let options = { min: conf.min || conf.is, max: conf.max || conf.is }; + if (!validator.isLength(value, options)) { if (conf.is) { throw new Error(`Value should be ${conf.is} characters long`); @@ -26,7 +27,7 @@ export const validators = { } } }, - numericality: function(value, conf) { + numericality: (value, conf) => { if (conf.int) { if (!validator.isInt(value)) throw new Error(`Value should be integer`);