Skip to content

Commit

Permalink
Don't include all ramda in bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Augustin Le Fèvre committed Aug 14, 2017
1 parent 578ab9e commit b369ba4
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 79 deletions.
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
"url": "git+https://github.com/kilix/functional-validation.git"
},
"keywords": [],
"files": ["lib"],
"files": [
"lib"
],
"jest": {
"testPathIgnorePatterns": ["<rootDir>/src/__tests__/typingTests.js"]
"testPathIgnorePatterns": [
"<rootDir>/src/__tests__/typingTests.js"
]
},
"author": "[email protected]",
"license": "MIT",
Expand All @@ -33,7 +37,7 @@
"babel-cli-flow": "^1.0.0",
"babel-core": "^6.23.1",
"babel-eslint": "^7.2.0",
"babel-loader": "^6.3.2",
"babel-loader": "^7.1.1",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-env": "^1.1.8",
Expand All @@ -44,17 +48,17 @@
"eslint-plugin-flowtype": "^2.35.0",
"eslint-plugin-import": "2.7",
"eslint-plugin-jsx-a11y": "5.1.1",
"eslint-plugin-react": "7.2",
"flow-bin": "^0.52.0",
"husky": "^0.14.3",
"jest": "^20.0.4",
"lint-staged": "^4.0.3",
"npm-run-all": "^4.0.2",
"prettier": "^1.5.3",
"rimraf": "^2.6.1",
"webpack": "^2.2.1"
"webpack": "^3.5.4"
},
"dependencies": {
"eslint-plugin-react": "7.2",
"ramda": "^0.23.0"
},
"peerDependencies": {},
Expand Down
7 changes: 4 additions & 3 deletions src/createValidation.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// @flow
import Ramda from 'ramda';
import pipe from 'ramda/src/pipe';
import whenR from 'ramda/src/when';

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;
const when: any = whenR;

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

export default createValidation;
13 changes: 7 additions & 6 deletions src/createValidations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import Ramda from 'ramda';
import curry from 'ramda/src/curry';
import pathR from 'ramda/src/path';

import createValidation from './createValidation';
import { testMissingValue } from './fieldValidators';
Expand All @@ -8,16 +9,16 @@ 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 path: any = pathR;

const createNestedValidation: CreateNestedValidation = R.curry(
const createNestedValidation: CreateNestedValidation = curry(
(validateField: () => string | null, fieldPath: $ReadOnlyArray<string>) =>
createValidation(fieldPath.join('.'), R.path(fieldPath), validateField),
createValidation(fieldPath.join('.'), path(fieldPath), validateField),
);

const createSimpleValidation: CreateSimpleValidation = R.curry(
const createSimpleValidation: CreateSimpleValidation = curry(
(validateField: () => string | null, field: string) =>
createValidation(field, R.prop(field), validateField),
createValidation(field, model => model[field], validateField),
);

type ValidateRequired = <T: Object>(field: $Keys<T>) => (model: T) => ErrorT | null;
Expand Down
8 changes: 4 additions & 4 deletions src/fieldValidators.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import R from 'ramda';
import curry from 'ramda/src/curry';

/**
* @desc Test if a value is ok for a required field.
Expand Down Expand Up @@ -56,7 +56,7 @@ const testOnlyLetters = (value: ?string) =>
* @param {Number} length - The length
* @return {?string} 'wrongLength' if the item doesn't have the specified length, null if it has
*/
const testLength = R.curry(
const testLength = curry(
(length, value) => (!value || !length || value.length === length ? null : 'wrongLength'),
);

Expand All @@ -77,7 +77,7 @@ const testEmailFormat = (value: ?string) => {
* @param {?string|Array} value - The array or string
* @return {?string} 'wrongLength' if the value is too long, null if it is correct
*/
const checkMaxLength = R.curry(
const checkMaxLength = curry(
(maxLength: number, input: ?string) =>
(typeof input !== 'string' && !Array.isArray(input)) || input.length <= maxLength
? null
Expand All @@ -91,7 +91,7 @@ const checkMaxLength = R.curry(
* @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(
const isBetween = curry(
(min: number, max: number, input: ?number) =>
typeof input === 'number' && (input < min || input > max) ? 'wrongValue' : null,
);
Expand Down
4 changes: 1 addition & 3 deletions src/runConditionalValidation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// @flow
import R from 'ramda';

import validateModel from './validateModel';
import type { ModelValidatorT, ValidationsT } from './validateModel';

function runConditionalValidation<T>(
condition: (model: T) => boolean,
validations: ValidationsT<T>,
): ModelValidatorT<T> {
return R.ifElse(condition, validateModel(validations), () => []);
return (model: T) => (condition(model) ? validateModel(validations)(model) : []);
}

export default runConditionalValidation;
14 changes: 10 additions & 4 deletions src/validateModel.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// @flow
import Ramda from 'ramda';
import pipe from 'ramda/src/pipe';
import memoize from 'ramda/src/memoize';
import flattenR from 'ramda/src/flatten';
import mapR from 'ramda/src/map';
import filterR from 'ramda/src/filter';

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

export type ErrorT = { field: string, error: string };
type MaybeErrorT = ErrorT | null;
Expand All @@ -14,8 +20,8 @@ export type ModelValidatorT<T> = (model: T) => $ReadOnlyArray<ErrorT>;
export type ValidationsT<T> = $ReadOnlyArray<ValidationT<T>>;

function validateModel<T>(validations: ValidationsT<T>): ModelValidatorT<T> {
return R.memoize((model: T): $ReadOnlyArray<ErrorT> =>
R.pipe(R.map(validation => validation(model)), R.flatten, R.filter(Boolean))(validations),
return memoize((model: T): $ReadOnlyArray<ErrorT> =>
pipe(map(validation => validation(model)), flatten, filter(Boolean))(validations),
);
}

Expand Down
Loading

0 comments on commit b369ba4

Please sign in to comment.