Skip to content

Commit

Permalink
improve min/max number parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
franleplant committed Oct 12, 2016
1 parent c26452e commit 077df92
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 192 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/elementMin.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe(validatorKey, () => {

inputTypesAndValues
.forEach(([inputType, min, successValue, failureValue]) => {
describe(`<input type="${inputType}" ${validatorKey}=${min} />" with failureValue = ${failureValue}}`, () => {
describe(`<input type="${inputType}" ${validatorKey}=${min} />"`, () => {
const successConfig = {type: 'input', inputType: inputType, validator: {[validatorKey]: min}, value: successValue, error: false}
const failureConfig = {type: 'input', inputType: inputType, validator: {[validatorKey]: min}, value: failureValue, error: true}
controlIntialStateTest(failureConfig)
Expand Down
187 changes: 95 additions & 92 deletions src/officialValidators/max.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,133 +11,136 @@ export const supportedInputTypes = [
]

export default function maxValidator(control) {
if (!control.isInputType(supportedInputTypes)) {
console.warn(`Validator: "max" not supported for type ${JSON.stringify(control, null, 2)}`)
return false
}

if (control.isInputType(supportedInputTypes)) {
const max = control.validationRules.max
const value = control.value
let max = control.validationRules.max
let value = control.value

if (control.isInputType(['range', 'number'])) {
if (!Number.isFinite(max)) {
console.error(`Validator: "max" should have a valid number as value. Found ${max}. In ${control.elementType}`)
return false
}
// Special case for empty values. This is the job of the `required` validator
if (!value) {
return false;
}

if (!Number.isFinite(value)) {
return true;
if (control.isInputType(['range', 'number'])) {
try {
max = parseInt(max, 10);
value = parseInt(value, 10);

if (!Number.isFinite(max)) {
throw new Error('not finite');
}

return value > max
} catch (err) {
console.error(`Validator: "max" should have a valid number as value. Found ${max}. In ${control.elementType}`)
return false;
}

if (control.isInputType(['date', 'datetime-local'])) {
const maxDate = Date.parse(max)
const valueDate = Date.parse(value)
if (Number.isNaN(maxDate)) {
console.error(`Validator: "max" should have a valid date as value. Found ${max}. In ${JSON.stringify(control, null, 2)}`)
return false
}
if (!Number.isFinite(value)) {
return true;
}

if (Number.isNaN(valueDate)) {
return true;
}
return value > max
}

return valueDate > maxDate;
if (control.isInputType(['date', 'datetime-local'])) {
const maxDate = Date.parse(max)
const valueDate = Date.parse(value)
if (Number.isNaN(maxDate)) {
console.error(`Validator: "max" should have a valid date as value. Found ${max}. In ${JSON.stringify(control, null, 2)}`)
return false
}

if (control.isInputType('month')) {
const [vYear, vMonth] = parseMonth(value);
const [mYear, mMonth] = parseMonth(max);
if (Number.isNaN(valueDate)) {
return true;
}

// Invalid max
if (!mYear || !mMonth) {
console.error(`Validator: "max" should have a valid month as value. Found ${max}. In ${JSON.stringify(control, null, 2)}`)
return false
}
return valueDate > maxDate;
}

// Invalid week
if (!vYear || !vMonth) {
return true
}
if (control.isInputType('month')) {
const [vYear, vMonth] = parseMonth(value);
const [mYear, mMonth] = parseMonth(max);

// Invalid max
if (!mYear || !mMonth) {
console.error(`Validator: "max" should have a valid month as value. Found ${max}. In ${JSON.stringify(control, null, 2)}`)
return false
}

// Invalid week
if (!vYear || !vMonth) {
return true
}

let error = false;

if (vYear > mYear) {
error = true;
} else if (vYear < mYear) {
error = false;
} else if (vYear === mYear) {
error = vMonth > mMonth
};

let error = false;

if (vYear > mYear) {
error = true;
} else if (vYear < mYear) {
error = false;
} else if (vYear === mYear) {
error = vMonth > mMonth
};

return error;
}

if (control.isInputType('week')) {
const [vYear, vWeek] = parseWeek(value);
const [mYear, mWeek] = parseWeek(max);

// Invalid max
if (!mYear || !mWeek) {
console.error(`Validator: "max" should have a valid week as value. Found ${max}. In ${JSON.stringify(control, null, 2)}`)
return false
}
return error;
}

// Invalid week
if (!vYear || !vWeek) {
return true
}
if (control.isInputType('week')) {
const [vYear, vWeek] = parseWeek(value);
const [mYear, mWeek] = parseWeek(max);

let error = false;
// Invalid max
if (!mYear || !mWeek) {
console.error(`Validator: "max" should have a valid week as value. Found ${max}. In ${JSON.stringify(control, null, 2)}`)
return false
}

if (vYear > mYear) {
error = true;
} else if (vYear < mYear) {
error = false;
} else if (vYear === mYear) {
error = vWeek > mWeek
};
// Invalid week
if (!vYear || !vWeek) {
return true
}

let error = false;

if (vYear > mYear) {
error = true;
} else if (vYear < mYear) {
error = false;
} else if (vYear === mYear) {
error = vWeek > mWeek
};

return error;
}

if (control.isInputType('time')) {
// Hack to make parsing times easier
// TODO: review this
const baseDate = "1970-01-01"
const maxDate = Date.parse(baseDate + ' ' + max)
const valueDate = Date.parse(baseDate + ' ' + value)
if (Number.isNaN(maxDate)) {
console.error(`Validator: "max" should have a valid time as value. Found ${max}. In ${JSON.stringify(control, null, 2)}`)
return false
}

// TODO: do we need to validate if the date is invalid
if (Number.isNaN(valueDate)) {
return true;
}
return error;
}

if (control.isInputType('time')) {
// Hack to make parsing times easier
// TODO: review this
const baseDate = "1970-01-01"
const maxDate = Date.parse(baseDate + ' ' + max)
const valueDate = Date.parse(baseDate + ' ' + value)
if (Number.isNaN(maxDate)) {
console.error(`Validator: "max" should have a valid time as value. Found ${max}. In ${JSON.stringify(control, null, 2)}`)
return false
}

//console.warn(`
//max time
//=========

//max = ${max}
//value = ${value} ${typeof value}
// TODO: do we need to validate if the date is invalid
if (Number.isNaN(valueDate)) {
return true;
}

//maxDate = ${maxDate}
//valueDate = ${valueDate}

//${JSON.stringify(control, null, 2)}
//`)

return valueDate > maxDate;
}
return valueDate > maxDate;
}

console.warn(`Validator: "max" not supported for type ${JSON.stringify(control, null, 2)}`)
Expand Down
36 changes: 36 additions & 0 deletions src/officialValidators/max.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import maxValidator from './max';
import Control from '../control';



describe('validator: max', () => {
it(`should not return an error with value='' `, () => {
const control = new Control({
type: 'input',
props:{
type: 'number',
name: 'myField',
value: '',
max: 5
}
});

const error = maxValidator(control);
expect(error).toBe(false);
})

it(`should work with strings as both value and max `, () => {
const control = new Control({
type: 'input',
props:{
type: 'number',
name: 'myField',
value: '6',
max: '5'
}
});

const error = maxValidator(control);
expect(error).toBe(true);
})
})
Loading

0 comments on commit 077df92

Please sign in to comment.