Skip to content

Commit

Permalink
Loose ramda typings
Browse files Browse the repository at this point in the history
  • Loading branch information
Augustin Le Fèvre committed Aug 14, 2017
1 parent 51c9d85 commit 578ab9e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "npm-run-all --parallel build:*",
"build:main": "babel src --out-dir lib --ignore '**/__tests__/**'",
"build:umd": "webpack --output-filename index.umd.js -p",
"build:flow": "babel-flow src --out-dir lib",
"build:flow": "babel-flow src --out-dir lib --ignore '**/__tests__/**'",
"flow": "flow check",
"test": "jest --watch",
"test:ci": "jest --coverage && codecov",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/fieldValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,5 @@ it('isBetween', () => {
expect(isBetweenZeroAndTen(10)).toBe(null);
expect(isBetweenZeroAndTen(-1)).toBe('wrongValue');
expect(isBetweenZeroAndTen(11)).toBe('wrongValue');
expect(isBetweenZeroAndTen(null)).toBe(null);
});
8 changes: 6 additions & 2 deletions src/createValidation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// @flow
import R from 'ramda';
import Ramda from 'ramda';

import type { ErrorT } from './validateModel';

// ramda flow-typed typings are not always correct, and use Array, while this library uses
// $ReadOnlyArray
const R: any = Ramda;

function createValidation<M, T>(
field: string,
getField: (model: M) => T,
validateField: (field: T) => string | null,
): (M) => ErrorT | null {
): M => ErrorT | null {
return R.pipe(getField, validateField, R.when(Boolean, error => ({ field, error })));
}

Expand Down
6 changes: 5 additions & 1 deletion src/createValidations.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// @flow
import R from 'ramda';
import Ramda from 'ramda';

import createValidation from './createValidation';
import { testMissingValue } from './fieldValidators';
import type { ErrorT } from './validateModel';
import type { CreateSimpleValidation, CreateNestedValidation } from './types';

// ramda flow-typed typings are not always correct, and use Array, while this library uses
// $ReadOnlyArray
const R: any = Ramda;

const createNestedValidation: CreateNestedValidation = R.curry(
(validateField: () => string | null, fieldPath: $ReadOnlyArray<string>) =>
createValidation(fieldPath.join('.'), R.path(fieldPath), validateField),
Expand Down
9 changes: 6 additions & 3 deletions src/fieldValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ const checkMaxLength = R.curry(

/**
* @desc Test if a number is contained between two others (included)
* @param {?number} value - The array or string
* @return {?string} 'wrongValue' if the value is outside of the boundaries, null if not
* @param {number} min - The lower bound (exluded)
* @param {number} max - The upper bound (excluded)
* @param {?number} value - The value to test
* @return {?string} 'input' if the value is outside of the boundaries, null if not
*/
const isBetween = R.curry(
(min: number, max: number, input) => (input < min || input > max ? 'wrongValue' : null),
(min: number, max: number, input: ?number) =>
typeof input === 'number' && (input < min || input > max) ? 'wrongValue' : null,
);

export {
Expand Down
9 changes: 3 additions & 6 deletions src/getFieldError.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// @flow
import R from 'ramda';
import type { ErrorT } from './validateModel';

const getFieldError = (errors: $ReadOnlyArray<ErrorT>) => (field: string): $ReadOnlyArray<string> =>
errors.filter(R.propEq('field', field)).map(R.prop('error'));
errors.filter(error => error.field === field).map(error => error.error);

const getFieldSpecificErrors = (
errors: $ReadOnlyArray<ErrorT>,
fieldName: string,
): $ReadOnlyArray<ErrorT> =>
errors.filter(({ field, error }) => field === fieldName && error !== 'missing');

const fieldHasSpecificErrors: (
errors: $ReadOnlyArray<ErrorT>,
fieldName: string,
) => boolean = R.pipe(getFieldSpecificErrors, R.isEmpty, R.not);
const fieldHasSpecificErrors = (errors: $ReadOnlyArray<ErrorT>, fieldName: string): boolean =>
getFieldSpecificErrors(errors, fieldName).length > 0;

export { fieldHasSpecificErrors, getFieldError, getFieldSpecificErrors };
6 changes: 5 additions & 1 deletion src/validateModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// @flow
import R from 'ramda';
import Ramda from 'ramda';

// ramda flow-typed typings are not always correct, and use Array, while this library uses
// $ReadOnlyArray
const R: any = Ramda;

export type ErrorT = { field: string, error: string };
type MaybeErrorT = ErrorT | null;
Expand Down

0 comments on commit 578ab9e

Please sign in to comment.