diff --git a/package.json b/package.json index 5a1f41c2..4f5501dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "baseui-final-form", - "version": "3.0.13", + "version": "3.0.14", "main": "index.js", "module": "index.es.js", "author": "KahWee Teng ", diff --git a/src/validate/__tests__/validate.js b/src/validate/__tests__/validate.js index 0bf4e974..758cc08b 100644 --- a/src/validate/__tests__/validate.js +++ b/src/validate/__tests__/validate.js @@ -70,6 +70,8 @@ describe('validate', () => { expect(v.required(true)).toBeUndefined(); expect(v.required('hello')).toBeUndefined(); expect(v.required('-')).toBeUndefined(); + expect(v.required([])).toBeString(); + expect(v.required(['String'])).toBeUndefined(); }); it('should check "email" boundaries', () => { @@ -89,10 +91,29 @@ describe('validate', () => { expect(v.numeric('1.23')).toBeUndefined(); }); + it('should check "integer" boundaries', () => { + expect(v.integer('hello')).toBeString(); + expect(v.integer('-')).toBeString(); + expect(v.integer('0')).toBeUndefined(); + expect(v.integer(0)).toBeUndefined(); + expect(v.integer('1028801')).toBeUndefined(); + expect(v.integer(1028801)).toBeUndefined(); + expect(v.integer(-1)).toBeUndefined(); + expect(v.integer('-1')).toBeUndefined(); + expect(v.integer(1.23)).toBeString(); + expect(v.integer('1.23')).toBeString(); + }); + it('should check "uuid" boundaries', () => { expect(v.uuid('83cd3add-8a17-459e-b1cb-0becd3891f3d')).toBeUndefined(); expect(v.uuid('ecf0bd9f-143f-4cb8-bf5c-0f6b209bb020')).toBeUndefined(); expect(v.uuid('1.23')).toBeString(); expect(v.uuid('')).toBeUndefined(); }); + + it('composeValidators', () => { + const validate = v.composeValidators(v.required, v.integer); + expect(validate('2222')).toBeUndefined(); + expect(validate()).toBeString(); + }); }); diff --git a/src/validate/index.js b/src/validate/index.js index dbfdac37..2458dc79 100644 --- a/src/validate/index.js +++ b/src/validate/index.js @@ -37,7 +37,9 @@ export function maxValue(max: number) { }; } -export function required(value: ?string | ?number | ?boolean) { +export function required( + value: ?string | ?number | ?boolean | ?Array +) { if (Array.isArray(value) && value.length === 0) { return 'Required'; } else if (value === undefined || value === null || value === '') { @@ -46,7 +48,7 @@ export function required(value: ?string | ?number | ?boolean) { return; } -export function uuid(value: string) { +export function uuid(value: ?string) { return value && !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test( value @@ -55,12 +57,27 @@ export function uuid(value: string) { : undefined; } -export function email(value: string) { +export function email(value: ?string) { return value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value) ? 'Invalid email address' : undefined; } -export function numeric(value: ?string | number) { +export function numeric(value: ?string | ?number) { return value && !isNumeric(value) ? 'Must be a number' : undefined; } + +export function integer(value: ?string | ?number) { + if (value) { + const number = Number(value); + if (isNaN(number)) { + return 'Must be a number'; + } else { + return Number.isInteger(number) ? undefined : 'Must be an integer'; + } + } +} + +export type ValueT = ?mixed | ?Array; +export const composeValidators = (...validators: any) => (value: ValueT) => + validators.reduce((error, validator) => error || validator(value), undefined);