Skip to content

Commit

Permalink
Merge pull request #17 from Kilix/add-trim-in-test-missing-field
Browse files Browse the repository at this point in the history
Add .trim() in testMissingValue
  • Loading branch information
T0din authored Jul 1, 2020
2 parents be0a85c + 264b404 commit b7ca915
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 52 deletions.
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "@kilix/functional-validation",
"version": "2.3.3",
"version": "2.3.5",
"description": "Lightweight and customisable validation, built through composition",
"main": "lib/index.js",
"scripts": {
"prebuild": "rimraf lib/",
"build:flow":
"mkdir lib && pax -rw -pe -s/.js/.js.flow/ src/ lib/ && cp -r lib/src/ lib/ && rm -rf lib/src",
"build:flow": "mkdir lib && pax -rw -pe -s/.js/.js.flow/ src/ lib/ && cp -r lib/src/ lib/ && rm -rf lib/src",
"build": "yarn build:flow && yarn build:main && yarn build:umd",
"build:umd": "webpack --output-filename index.umd.js -p",
"build:main": "babel src --out-dir lib --ignore '**/__tests__/**'",
Expand All @@ -16,17 +15,20 @@
"lint": "eslint ./src",
"precommit": "lint-staged",
"prepublishOnly": "npm run build",
"format":
"prettier --trailing-comma all --single-quote --tab-width 4 --print-width 100 --write 'src/**/*.js'"
"format": "prettier --trailing-comma all --single-quote --tab-width 4 --print-width 100 --write 'src/**/*.js'"
},
"repository": {
"type": "git",
"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 Down
4 changes: 4 additions & 0 deletions src/__tests__/fieldValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ it('testMissingValue', () => {
actual = testMissingValue(false);
expected = null;
expect(actual).toBe(expected);

actual = testMissingValue(' ');
expected = 'missing';
expect(actual).toBe(expected);
});

it('testDateAfter', () => {
Expand Down
93 changes: 48 additions & 45 deletions src/fieldValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,74 @@
import curry from 'ramda/src/curry';

/**
* @desc Test if a value is ok for a required field.
* If the field has a formattedAddress key, it's a location, so we make sure that this key is set.
* If field is an array, check length.
* @param {Any} field - The value of the field
* @return {bool} True if the value is falsy
*/
* @desc Test if a value is ok for a required field.
* If the field has a formattedAddress key, it's a location, so we make sure that this key is set.
* If field is an array, check length.
* @param {Any} field - The value of the field
* @return {bool} True if the value is falsy
*/
const testMissingValue = (field: any): string | null =>
(!field && field !== false) || field.length === 0 || field.formattedAddress === null
(!field && field !== false) ||
field.length === 0 ||
field.formattedAddress === null ||
(typeof field === 'string' && !field.trim())
? 'missing'
: null;

/**
* @desc Check if a date is after the given date
* @param {Date|moment} date - The date to test
* @param {Date|moment} limit - The reference date
* @return {string} "before" if the date is before the limit, and both are specified, null if not
*/
* @desc Check if a date is after the given date
* @param {Date|moment} date - The date to test
* @param {Date|moment} limit - The reference date
* @return {string} "before" if the date is before the limit, and both are specified, null if not
*/
type DateT = string | Date | any; // Also handle moment objects, or object with a valueOf
const getDate = (date: DateT) => new Date(date.valueOf ? date.valueOf() : date);
type DatesT = { date: ?DateT, limit: ?DateT };
const testDateAfter = ({ date, limit }: DatesT) =>
!date || !limit || getDate(date) - getDate(limit) > 0 ? null : 'before';

/**
* @desc Check if a date is before the given date
* @param {Date|moment} date - The date to test
* @param {Date|moment} limit - The reference date
* @return {string} "after" if the date is after the limit, and both are specified, null if not
*/
* @desc Check if a date is before the given date
* @param {Date|moment} date - The date to test
* @param {Date|moment} limit - The reference date
* @return {string} "after" if the date is after the limit, and both are specified, null if not
*/
const testDateBefore = ({ date, limit }: DatesT) =>
!date || !limit || getDate(date) - getDate(limit) < 0 ? null : 'after';

/**
* @desc Test if a value is greater than another
* @param {Number} value - The value to test
* @param {Number} min - The value to be greater than
* @return {string} 'smaller' if the value is smaller, null if not
*/
* @desc Test if a value is greater than another
* @param {Number} value - The value to test
* @param {Number} min - The value to be greater than
* @return {string} 'smaller' if the value is smaller, null if not
*/
type GreaterThanT = { value: ?(string | number), min: ?(string | number) };
const testGreaterThan = ({ value, min }: GreaterThanT) =>
value == null || min == null || Number(value) >= Number(min) ? null : 'smaller';

/**
* @desc Test if a value is only letters
* @param {String} value - The value to test
* @return {?string} 'notOnlyLetters' if any character is not a letter, null if not
*/
* @desc Test if a value is only letters
* @param {String} value - The value to test
* @return {?string} 'notOnlyLetters' if any character is not a letter, null if not
*/
const testOnlyLetters = (value: ?string) =>
!value || /^[a-zA-Z]+$/.test(value) ? null : 'notOnlyLetters';

/**
* @desc Test if a string or an array has the proper length
* @param {String|Array} value - The value to test
* @param {Number} length - The length
* @return {?string} 'wrongLength' if the item doesn't have the specified length, null if it has
*/
* @desc Test if a string or an array has the proper length
* @param {String|Array} value - The value to test
* @param {Number} length - The length
* @return {?string} 'wrongLength' if the item doesn't have the specified length, null if it has
*/
const testLength = curry(
(length, value) => (!value || !length || value.length === length ? null : 'wrongLength'),
);

/**
* @desc Test if a value is a valid email address
* @param {?string} value - The value of the field
* @return {?string} 'notEmail' if the value is not a valid email address, null if it is
*/
* @desc Test if a value is a valid email address
* @param {?string} value - The value of the field
* @return {?string} 'notEmail' if the value is not a valid email address, null if it is
*/
const testEmailFormat = (value: ?string) => {
if (!value) return null;

Expand All @@ -76,10 +79,10 @@ const testEmailFormat = (value: ?string) => {
};

/**
* @desc Test an array or a string's max length
* @param {?string|Array} value - The array or string
* @return {?string} 'wrongLength' if the value is too long, null if it is correct
*/
* @desc Test an array or a string's max length
* @param {?string|Array} value - The array or string
* @return {?string} 'wrongLength' if the value is too long, null if it is correct
*/
const checkMaxLength = curry(
(maxLength: number, input: ?string) =>
(typeof input !== 'string' && !Array.isArray(input)) || input.length <= maxLength
Expand All @@ -88,12 +91,12 @@ const checkMaxLength = curry(
);

/**
* @desc Test if a number is contained between two others (included)
* @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
*/
* @desc Test if a number is contained between two others (included)
* @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 = curry(
(min: number, max: number, input: ?number) =>
typeof input === 'number' && (input < min || input > max) ? 'wrongValue' : null,
Expand Down

0 comments on commit b7ca915

Please sign in to comment.