Skip to content

Commit

Permalink
(feat) add integer validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kahwee committed Jul 2, 2020
1 parent dd869cf commit 14a7824
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <tengkahwee@gmail.com>",
Expand Down
21 changes: 21 additions & 0 deletions src/validate/__tests__/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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();
});
});
25 changes: 21 additions & 4 deletions src/validate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export function maxValue(max: number) {
};
}

export function required(value: ?string | ?number | ?boolean) {
export function required(
value: ?string | ?number | ?boolean | ?Array<?string>
) {
if (Array.isArray(value) && value.length === 0) {
return 'Required';
} else if (value === undefined || value === null || value === '') {
Expand All @@ -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
Expand All @@ -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<mixed>;
export const composeValidators = (...validators: any) => (value: ValueT) =>
validators.reduce((error, validator) => error || validator(value), undefined);

0 comments on commit 14a7824

Please sign in to comment.