From 264b404aa5c4daadee6b8a777fec89a2602d02b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Am=C3=A9lie=20Lef=C3=A8vre?= Date: Wed, 1 Jul 2020 15:28:54 +0200 Subject: [PATCH] Add .trim() in testMissingValue --- package.json | 16 +++--- src/__tests__/fieldValidators.js | 4 ++ src/fieldValidators.js | 93 ++++++++++++++++---------------- 3 files changed, 61 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index b90d564..c137d9e 100644 --- a/package.json +++ b/package.json @@ -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__/**'", @@ -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": ["/src/__tests__/typingTests.js"] + "testPathIgnorePatterns": [ + "/src/__tests__/typingTests.js" + ] }, "author": "aulefevre@kilix.fr", "license": "MIT", diff --git a/src/__tests__/fieldValidators.js b/src/__tests__/fieldValidators.js index 1197698..4cb55d9 100644 --- a/src/__tests__/fieldValidators.js +++ b/src/__tests__/fieldValidators.js @@ -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', () => { diff --git a/src/fieldValidators.js b/src/fieldValidators.js index 707d0cf..c480917 100644 --- a/src/fieldValidators.js +++ b/src/fieldValidators.js @@ -2,23 +2,26 @@ 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 }; @@ -26,47 +29,47 @@ 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; @@ -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 @@ -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,