Skip to content

Commit d4e44dd

Browse files
committed
fix url validation and add tests
1 parent eeb42ae commit d4e44dd

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

Diff for: src/process/validation/__tests__/Validator.test.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { expect } from 'chai';
2-
import { validateProcess } from '../index';
2+
import { interpolateErrors, validateProcess } from '../index';
33
import { rules } from "../rules";
44
import { simpleForm } from './fixtures/forms';
55
import { ProcessorType, ValidationScope } from 'types';
6+
import { FieldError, InterpolateErrorFn } from 'error';
67
import get from 'lodash/get';
78

89
it('Validator will throw the correct errors given a flat components array', async () => {
@@ -38,10 +39,16 @@ it('Validator will throw the correct errors given a flat components array', asyn
3839
});
3940

4041
it('Validation errors (FieldErrors) should include the rule name mapping in the "validator" param', async () => {
41-
let errors: string[] = [];
4242
const data = {
43-
requiredField: ''
43+
requiredField: '',
44+
maximumWords: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
45+
minimumWords: 'Hello',
46+
email: 'brendanb',
47+
url: 'htpigoogle',
48+
inputMask: 'hello, world',
49+
submit: false,
4450
};
51+
const result: Map<string, ReturnType<typeof interpolateErrors>> = new Map();
4552
for (let component of simpleForm.components) {
4653
const path = component.key;
4754
const scope: ValidationScope = { errors: [] };
@@ -55,9 +62,18 @@ it('Validation errors (FieldErrors) should include the rule name mapping in the
5562
processor: ProcessorType.Validate,
5663
rules
5764
});
58-
if (scope.errors) {
59-
errors = [...errors, ...scope.errors.map((error) => error.errorKeyOrMessage)];
60-
}
65+
result.set(path, interpolateErrors(scope.errors));
6166
}
62-
console.log(errors);
67+
expect(result.get('requiredField')).to.have.length(1);
68+
expect(result.get('requiredField')![0].context.validator).to.equal('required');
69+
expect(result.get('maximumWords')).to.have.length(1);
70+
expect(result.get('maximumWords')![0].context.validator).to.equal('maxWords');
71+
expect(result.get('minimumWords')).to.have.length(1);
72+
expect(result.get('minimumWords')![0].context.validator).to.equal('minWords');
73+
expect(result.get('email')).to.have.length(1);
74+
expect(result.get('email')![0].context.validator).to.equal('email');
75+
expect(result.get('url')).to.have.length(1);
76+
expect(result.get('url')![0].context.validator).to.equal('url');
77+
expect(result.get('inputMask')).to.have.length(1);
78+
expect(result.get('inputMask')![0].context.validator).to.equal('inputMask');
6379
});

Diff for: src/process/validation/rules/validateUrl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const validateUrlSync: RuleFnSync = (context: ValidationContext) => {
2222
if (!shouldValidate(context)) {
2323
return null;
2424
}
25-
const error = new FieldError('invalid_url', context);
25+
const error = new FieldError('invalid_url', context, 'url');
2626
if (typeof value !== 'string') {
2727
return error;
2828
}

Diff for: src/process/validation/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const interpolateErrors = (errors: FieldError[], lang: string = 'en') =>
7777
level: error.level,
7878
path: paths,
7979
context: {
80-
validator: context.processor,
80+
validator: error.ruleName,
8181
hasLabel: context.hasLabel,
8282
key: context.component.key,
8383
label: context.component.label || context.component.placeholder || context.component.key,

0 commit comments

Comments
 (0)