Skip to content

Commit

Permalink
dont validate official validators on empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
franleplant committed Oct 12, 2016
1 parent 077df92 commit d89612d
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/elementDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe(validatorKey, () => {
const validator = {[validatorKey]: true}
const successConfig = {type: 'input', inputType: validatorKey, validator, value: "2016-08-05", error: false}
const failureConfig = {type: 'input', inputType: validatorKey, validator, value: "fail", error: true}
const initialConfig = {type: 'input', inputType: validatorKey, validator, value: "", error: true}
const initialConfig = {type: 'input', inputType: validatorKey, validator, value: "", error: false}
describe(`<input type=${validatorKey} />"`, () => {
controlIntialStateTest(initialConfig)
controlOnChangeTest(failureConfig)
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/elementEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { controlOnChangeTest, controlIntialStateTest } from '../testTemplates'
describe('email', () => {
const validator = {email: true}
const successConfig = {type: 'input', inputType: 'email', validator, value: "a@ab.com", error: false}
const failureConfig = {type: 'input', inputType: 'email', validator, value: "", error: true}
const failureConfig = {type: 'input', inputType: 'email', validator, value: "", error: false}
describe(`<input type="email" />"`, () => {
controlIntialStateTest(failureConfig)
controlOnChangeTest(failureConfig)
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/elementMinLenght.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('minLength', () => {
.forEach(inputType => {
describe(`<input type="${inputType}" minLength=3 />"`, () => {
const successConfig = {type: 'input', inputType: inputType, validator, value: "okk", error: false}
const failureConfig = {type: 'input', inputType: inputType, validator, value: "", error: true}
const failureConfig = {type: 'input', inputType: inputType, validator, value: "", error: false}
controlIntialStateTest(failureConfig)
controlOnChangeTest(failureConfig)
controlOnChangeTest(successConfig)
Expand All @@ -23,7 +23,7 @@ describe('minLength', () => {

describe(`<textarea minLength=3 />"`, () => {
const successConfig = {type: 'textarea', validator, value: "okk", error: false}
const failureConfig = {type: 'textarea', validator, value: "", error: true}
const failureConfig = {type: 'textarea', validator, value: "", error: false}
controlIntialStateTest(failureConfig)
controlOnChangeTest(failureConfig)
controlOnChangeTest(successConfig)
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/elementNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe(validatorKey, () => {
const validator = {[validatorKey]: true}
const successConfig = {type: 'input', inputType: validatorKey, validator, value: 10, error: false}
const failureConfig = {type: 'input', inputType: validatorKey, validator, value: "fail", error: true}
const initialConfig = {type: 'input', inputType: validatorKey, validator, value: "", error: true}
const initialConfig = {type: 'input', inputType: validatorKey, validator, value: "", error: false}
describe(`<input type=${validatorKey} />"`, () => {
controlIntialStateTest(initialConfig)
controlOnChangeTest(failureConfig)
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/elementPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('pattern', () => {
.forEach(inputType => {
describe(`<input type="${inputType}" pattern="apple|banana" />"`, () => {
const successConfig = {type: 'input', inputType: inputType, validator, value: "banana", error: false}
const failureConfig = {type: 'input', inputType: inputType, validator, value: "", error: true}
const failureConfig = {type: 'input', inputType: inputType, validator, value: "", error: false}
controlIntialStateTest(failureConfig)
controlOnChangeTest(successConfig)
controlOnChangeTest(failureConfig)
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/elementRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe(validatorKey, () => {
const validator = {[validatorKey]: true}
const successConfig = {type: 'input', inputType: validatorKey, validator, value: 10, error: false}
const failureConfig = {type: 'input', inputType: validatorKey, validator, value: "fail", error: true}
const initialConfig = {type: 'input', inputType: validatorKey, validator, value: "", error: true}
const initialConfig = {type: 'input', inputType: validatorKey, validator, value: "", error: false}
describe(`<input type=${validatorKey} />"`, () => {
controlIntialStateTest(initialConfig)
controlOnChangeTest(failureConfig)
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/elementUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { controlOnChangeTest, controlIntialStateTest } from '../testTemplates'
describe('url', () => {
const validator = {url: true}
const successConfig = {type: 'input', inputType: 'url', validator, value: "www.google.com", error: false}
const failureConfig = {type: 'input', inputType: 'url', validator, value: "", error: true}
const failureConfig = {type: 'input', inputType: 'url', validator, value: "", error: false}
describe(`<input type="url" />"`, () => {
controlIntialStateTest(failureConfig)
controlOnChangeTest(failureConfig)
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('reform onSubmit', () => {
const errorMap = form.getErrorMap();
expect(errorMap[name1].required).toBe(true);
expect(errorMap[name2].required).toBe(true);
expect(errorMap[name2].email).toBe(true);
expect(errorMap[name2].email).toBe(false);
});

it('should be invalid when all the controls are invalid', () => {
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('reform onSubmit', () => {
const errorMap = form.getErrorMap();
expect(errorMap[name1].required).toBe(false);
expect(errorMap[name2].required).toBe(true);
expect(errorMap[name2].email).toBe(true);
expect(errorMap[name2].email).toBe(false);
})

it('should be invalid when some controls are invalid', () => {
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('reform submit mechanisms', () => {
const errorMap = form.getErrorMap();
expect(errorMap[name1].required).toBe(true);
expect(errorMap[name2].required).toBe(true);
expect(errorMap[name2].email).toBe(true);
expect(errorMap[name2].email).toBe(false);
});

it('should be invalid when all the controls are invalid', () => {
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('reform submit mechanisms', () => {
const errorMap = form.getErrorMap();
expect(errorMap[name1].required).toBe(true);
expect(errorMap[name2].required).toBe(true);
expect(errorMap[name2].email).toBe(true);
expect(errorMap[name2].email).toBe(false);
});

it('should be invalid when all the controls are invalid', () => {
Expand Down Expand Up @@ -230,7 +230,7 @@ describe('reform submit mechanisms', () => {
const errorMap = form.getErrorMap();
expect(errorMap[name1].required).toBe(true);
expect(errorMap[name2].required).toBe(true);
expect(errorMap[name2].email).toBe(true);
expect(errorMap[name2].email).toBe(false);
});

it('should be invalid when all the controls are invalid', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/officialValidators/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
// Example "#c82f13"
const re = /^#[0-9A-F]{6}$/;
export default function colorValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

return !re.test(control.value)
}
5 changes: 5 additions & 0 deletions src/officialValidators/date.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Official docs https://www.w3.org/TR/html5/infrastructure.html#valid-date-string

export default function dateValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const date = Date.parse(control.value);
return Number.isNaN(date);
}
5 changes: 5 additions & 0 deletions src/officialValidators/datetime-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

// Example "2016-08-10T11:01"
export default function datetimeValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const date = Date.parse(control.value)
return Number.isNaN(date)
}
5 changes: 5 additions & 0 deletions src/officialValidators/email.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

export default function emailValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

return !/.+@.+\..+/.test(control.value)
}
5 changes: 5 additions & 0 deletions src/officialValidators/minLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export const supportedInputTypes = [
]

export default function minLengthValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const condition =
control.isInputType(supportedInputTypes) ||
control.isFunctionType() ||
Expand Down
5 changes: 5 additions & 0 deletions src/officialValidators/month.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import { parseMonth } from '../utils';

export default function monthValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const value = control.value
const [ year, month ] = parseMonth(value)

Expand Down
5 changes: 5 additions & 0 deletions src/officialValidators/number.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Official docs https://www.w3.org/TR/html5/infrastructure.html

export default function numberValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const value = parseFloat(control.value)
return !Number.isFinite(value)
}
5 changes: 5 additions & 0 deletions src/officialValidators/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export const supportedInputTypes = ['text', 'search', 'url', 'tel', 'email', 'pa


export default function patternValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const condition = control.isInputType(supportedInputTypes) || control.isFunctionType()


Expand Down
5 changes: 5 additions & 0 deletions src/officialValidators/range.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Official docs https://www.w3.org/TR/html5/infrastructure.html

export default function rangeValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const value = parseFloat(control.value)
return !Number.isFinite(value)
}
5 changes: 5 additions & 0 deletions src/officialValidators/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const BASE_DATE = "1970-01-01"

// Example "02:00"
export default function timeValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const date = Date.parse(BASE_DATE + ' ' + control.value)
return Number.isNaN(date)
}
5 changes: 5 additions & 0 deletions src/officialValidators/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
const expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
const re = new RegExp(expression);
export default function urlValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

return !re.test(control.value)
}
5 changes: 5 additions & 0 deletions src/officialValidators/week.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { parseWeek } from '../utils';
// TODO: this needs to be improved since some years have 52 weeks
const MAX_WEEK = 53
export default function weekValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const value = control.value
const [year, week] = parseWeek(value)

Expand Down

0 comments on commit d89612d

Please sign in to comment.