revalidate
Advanced tools
Comparing version 0.2.1 to 0.3.0
104
CHANGELOG.md
@@ -0,1 +1,105 @@ | ||
## v0.3.0 | ||
### New Common Validators | ||
#### `isRequiredIf` | ||
`isRequiredIf` allows you to conditionally require a value based on the result | ||
of a predicate function. As long as your predicate function returns a truthy | ||
value, the field value will be required. | ||
This is perfect if you want to require a field if another field value is | ||
present: | ||
```js | ||
import { isRequiredIf } from 'revalidate'; | ||
const validator = combineValidators({ | ||
username: isRequiredIf( | ||
values => values && !values.useEmailAsUsername | ||
)('Username'), | ||
}); | ||
validator(); // { username: 'Username is required' } | ||
validator({ | ||
useEmailAsUsername: false, | ||
}); // { username: 'Username is required' } | ||
validator({ | ||
username: 'jfairbank', | ||
useEmailAsUsername: false, | ||
}); // {} | ||
validator({ | ||
useEmailAsUsername: true, | ||
}); // {}, so valid | ||
``` | ||
#### `matchesPattern` | ||
`matchesPattern` is a general purpose validator for validating values against | ||
arbitrary regex patterns. | ||
```js | ||
import { matchesPattern } from 'revalidate'; | ||
const isAlphabetic = matchesPattern(/^[A-Za-z]+$/)('Username'); | ||
isAlphabetic('abc'); // undefined, so valid | ||
isAlphabetic('123'); // 'Username must match pattern /^[A-Za-z]+$/' | ||
``` | ||
**Note:** `matchesPattern` does not require a value, so falsy values will pass. | ||
```js | ||
isAlphabetic(); // undefined because not required, so valid | ||
isAlphabetic(null); // undefined because not required, so valid | ||
isAlphabetic(''); // undefined because not required, so valid | ||
``` | ||
--- | ||
### Return multiple errors as an object | ||
In addition to returning multiple errors as an array with `composeValidators`, | ||
you can also return multiple errors as an object now. This is useful if you want | ||
to name your errors and react accordingly based on the type of error. Instead of | ||
passing in validators as a variadic number of arguments, pass in all validators | ||
inside an object. The keys of your object will the keys used in the error | ||
object. Don't forget to still supply the `multiple: true` option! | ||
```js | ||
import { createValidator, composeValidators } from 'revalidate'; | ||
const startsWithA = createValidator( | ||
message => value => { | ||
if (value && !/^A/.test(value)) { | ||
return message; | ||
} | ||
}, | ||
field => `${field} must start with A` | ||
); | ||
const endsWithC = createValidator( | ||
message => value => { | ||
if (value && !/C$/.test(value)) { | ||
return message; | ||
} | ||
}, | ||
field => `${field} must end with C` | ||
); | ||
const validator = composeValidators({ | ||
A: startsWithA, | ||
C: endsWithC | ||
})({ field: 'My Field', multiple: true }); | ||
validator('BBB'); | ||
// { | ||
// A: 'My Field must start with A', | ||
// C: 'My Field must end with C' | ||
// } | ||
``` | ||
## v0.2.1 | ||
@@ -2,0 +106,0 @@ |
'use strict'; | ||
exports.__esModule = true; | ||
exports.matchesField = exports.isRequired = exports.isOneOf = exports.isNumeric = exports.isAlphaNumeric = exports.isAlphabetic = exports.hasLengthLessThan = exports.hasLengthGreaterThan = exports.hasLengthBetween = exports.combineValidators = exports.composeValidators = exports.createValidator = undefined; | ||
exports.matchesPattern = exports.isRequiredIf = exports.matchesField = exports.isRequired = exports.isOneOf = exports.isNumeric = exports.isAlphaNumeric = exports.isAlphabetic = exports.hasLengthLessThan = exports.hasLengthGreaterThan = exports.hasLengthBetween = exports.combineValidators = exports.composeValidators = exports.createValidator = undefined; | ||
var _createValidator = require('./createValidator'); | ||
var _createValidator2 = require('./createValidator'); | ||
var _createValidator2 = _interopRequireDefault(_createValidator); | ||
var _createValidator3 = _interopRequireDefault(_createValidator2); | ||
var _composeValidators = require('./composeValidators'); | ||
var _composeValidators2 = require('./composeValidators'); | ||
var _composeValidators2 = _interopRequireDefault(_composeValidators); | ||
var _composeValidators3 = _interopRequireDefault(_composeValidators2); | ||
var _combineValidators = require('./combineValidators'); | ||
var _combineValidators2 = require('./combineValidators'); | ||
var _combineValidators2 = _interopRequireDefault(_combineValidators); | ||
var _combineValidators3 = _interopRequireDefault(_combineValidators2); | ||
var _hasLengthBetween = require('./validators/hasLengthBetween'); | ||
var _hasLengthBetween2 = require('./validators/hasLengthBetween'); | ||
var _hasLengthBetween2 = _interopRequireDefault(_hasLengthBetween); | ||
var _hasLengthBetween3 = _interopRequireDefault(_hasLengthBetween2); | ||
var _hasLengthGreaterThan = require('./validators/hasLengthGreaterThan'); | ||
var _hasLengthGreaterThan2 = require('./validators/hasLengthGreaterThan'); | ||
var _hasLengthGreaterThan2 = _interopRequireDefault(_hasLengthGreaterThan); | ||
var _hasLengthGreaterThan3 = _interopRequireDefault(_hasLengthGreaterThan2); | ||
var _hasLengthLessThan = require('./validators/hasLengthLessThan'); | ||
var _hasLengthLessThan2 = require('./validators/hasLengthLessThan'); | ||
var _hasLengthLessThan2 = _interopRequireDefault(_hasLengthLessThan); | ||
var _hasLengthLessThan3 = _interopRequireDefault(_hasLengthLessThan2); | ||
var _isAlphabetic = require('./validators/isAlphabetic'); | ||
var _isAlphabetic2 = require('./validators/isAlphabetic'); | ||
var _isAlphabetic2 = _interopRequireDefault(_isAlphabetic); | ||
var _isAlphabetic3 = _interopRequireDefault(_isAlphabetic2); | ||
var _isAlphaNumeric = require('./validators/isAlphaNumeric'); | ||
var _isAlphaNumeric2 = require('./validators/isAlphaNumeric'); | ||
var _isAlphaNumeric2 = _interopRequireDefault(_isAlphaNumeric); | ||
var _isAlphaNumeric3 = _interopRequireDefault(_isAlphaNumeric2); | ||
var _isNumeric = require('./validators/isNumeric'); | ||
var _isNumeric2 = require('./validators/isNumeric'); | ||
var _isNumeric2 = _interopRequireDefault(_isNumeric); | ||
var _isNumeric3 = _interopRequireDefault(_isNumeric2); | ||
var _isOneOf = require('./validators/isOneOf'); | ||
var _isOneOf2 = require('./validators/isOneOf'); | ||
var _isOneOf2 = _interopRequireDefault(_isOneOf); | ||
var _isOneOf3 = _interopRequireDefault(_isOneOf2); | ||
var _isRequired = require('./validators/isRequired'); | ||
var _isRequired2 = require('./validators/isRequired'); | ||
var _isRequired2 = _interopRequireDefault(_isRequired); | ||
var _isRequired3 = _interopRequireDefault(_isRequired2); | ||
var _matchesField = require('./validators/matchesField'); | ||
var _matchesField2 = require('./validators/matchesField'); | ||
var _matchesField2 = _interopRequireDefault(_matchesField); | ||
var _matchesField3 = _interopRequireDefault(_matchesField2); | ||
var _isRequiredIf2 = require('./validators/isRequiredIf'); | ||
var _isRequiredIf3 = _interopRequireDefault(_isRequiredIf2); | ||
var _matchesPattern2 = require('./validators/matchesPattern'); | ||
var _matchesPattern3 = _interopRequireDefault(_matchesPattern2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.createValidator = _createValidator2.default; | ||
exports.composeValidators = _composeValidators2.default; | ||
exports.combineValidators = _combineValidators2.default; | ||
exports.hasLengthBetween = _hasLengthBetween2.default; | ||
exports.hasLengthGreaterThan = _hasLengthGreaterThan2.default; | ||
exports.hasLengthLessThan = _hasLengthLessThan2.default; | ||
exports.isAlphabetic = _isAlphabetic2.default; | ||
exports.isAlphaNumeric = _isAlphaNumeric2.default; | ||
exports.isNumeric = _isNumeric2.default; | ||
exports.isOneOf = _isOneOf2.default; | ||
exports.isRequired = _isRequired2.default; | ||
exports.matchesField = _matchesField2.default; | ||
exports.createValidator = _createValidator3.default; | ||
exports.composeValidators = _composeValidators3.default; | ||
exports.combineValidators = _combineValidators3.default; | ||
exports.hasLengthBetween = _hasLengthBetween3.default; | ||
exports.hasLengthGreaterThan = _hasLengthGreaterThan3.default; | ||
exports.hasLengthLessThan = _hasLengthLessThan3.default; | ||
exports.isAlphabetic = _isAlphabetic3.default; | ||
exports.isAlphaNumeric = _isAlphaNumeric3.default; | ||
exports.isNumeric = _isNumeric3.default; | ||
exports.isOneOf = _isOneOf3.default; | ||
exports.isRequired = _isRequired3.default; | ||
exports.matchesField = _matchesField3.default; | ||
exports.isRequiredIf = _isRequiredIf3.default; | ||
exports.matchesPattern = _matchesPattern3.default; |
@@ -12,20 +12,48 @@ 'use strict'; | ||
function buildErrorsArray(validators, validate) { | ||
return validators.reduce(function (errors, validator) { | ||
var errorMessage = validate(validator); | ||
if (errorMessage) { | ||
errors.push(errorMessage); | ||
} | ||
return errors; | ||
}, []); | ||
} | ||
function buildErrorsObject(validators, validate) { | ||
return Object.keys(validators).reduce(function (errors, key) { | ||
var validator = validators[key]; | ||
var errorMessage = validate(validator); | ||
if (errorMessage) { | ||
errors[key] = errorMessage; | ||
} | ||
return errors; | ||
}, {}); | ||
} | ||
function createValidatorWithMultipleErrors(validators, sharedConfig) { | ||
var buildErrors = void 0; | ||
var finalValidators = void 0; | ||
if (typeof validators[0] === 'object') { | ||
buildErrors = buildErrorsObject; | ||
finalValidators = validators[0]; | ||
} else { | ||
buildErrors = buildErrorsArray; | ||
finalValidators = validators; | ||
} | ||
return function composedValidator(value, allValues) { | ||
return validators.reduce(function (errors, validator) { | ||
var errorMessage = void 0; | ||
return buildErrors(finalValidators, function (validator) { | ||
if ((0, _isValueValidator2.default)(validator)) { | ||
errorMessage = validator(value, allValues); | ||
} else { | ||
errorMessage = validator(sharedConfig, value, allValues); | ||
return validator(value, allValues); | ||
} | ||
if (errorMessage) { | ||
errors.push(errorMessage); | ||
} | ||
return errors; | ||
}, []); | ||
return validator(sharedConfig, value, allValues); | ||
}); | ||
}; | ||
} |
@@ -6,13 +6,13 @@ 'use strict'; | ||
var _fillObjectFromPath = require('./fillObjectFromPath'); | ||
var _fillObjectFromPath2 = require('./fillObjectFromPath'); | ||
var _fillObjectFromPath2 = _interopRequireDefault(_fillObjectFromPath); | ||
var _fillObjectFromPath3 = _interopRequireDefault(_fillObjectFromPath2); | ||
var _parseFieldName = require('./parseFieldName'); | ||
var _parseFieldName2 = require('./parseFieldName'); | ||
var _parseFieldName2 = _interopRequireDefault(_parseFieldName); | ||
var _parseFieldName3 = _interopRequireDefault(_parseFieldName2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.fillObjectFromPath = _fillObjectFromPath2.default; | ||
exports.parseFieldName = _parseFieldName2.default; | ||
exports.fillObjectFromPath = _fillObjectFromPath3.default; | ||
exports.parseFieldName = _parseFieldName3.default; |
@@ -12,2 +12,2 @@ 'use strict'; | ||
var VALUE_VALIDATOR_SYMBOL = exports.VALUE_VALIDATOR_SYMBOL = (0, _sym2.default)('VALUE_VALIDATOR'); | ||
var VALUE_VALIDATOR_SYMBOL = exports.VALUE_VALIDATOR_SYMBOL = (0, _sym2.default)('VALUE_VALIDATOR'); /* eslint-disable import/prefer-default-export */ |
@@ -5,18 +5,10 @@ 'use strict'; | ||
var _createValidator = require('../createValidator'); | ||
var _internalMatchesPattern = require('../internal/validators/internalMatchesPattern'); | ||
var _createValidator2 = _interopRequireDefault(_createValidator); | ||
var _internalMatchesPattern2 = _interopRequireDefault(_internalMatchesPattern); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var regex = /^[A-Za-z]+$/; | ||
exports.default = (0, _createValidator2.default)(function (message) { | ||
return function (value) { | ||
if (value && !regex.test(value)) { | ||
return message; | ||
} | ||
}; | ||
}, function (field) { | ||
exports.default = (0, _internalMatchesPattern2.default)(/^[A-Za-z]+$/, function (field) { | ||
return field + ' must be alphabetic'; | ||
}); |
@@ -5,18 +5,10 @@ 'use strict'; | ||
var _createValidator = require('../createValidator'); | ||
var _internalMatchesPattern = require('../internal/validators/internalMatchesPattern'); | ||
var _createValidator2 = _interopRequireDefault(_createValidator); | ||
var _internalMatchesPattern2 = _interopRequireDefault(_internalMatchesPattern); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var regex = /^[0-9A-Za-z]+$/; | ||
exports.default = (0, _createValidator2.default)(function (message) { | ||
return function (value) { | ||
if (value && !regex.test(value)) { | ||
return message; | ||
} | ||
}; | ||
}, function (field) { | ||
exports.default = (0, _internalMatchesPattern2.default)(/^[0-9A-Za-z]+$/, function (field) { | ||
return field + ' must be alphanumeric'; | ||
}); |
@@ -5,18 +5,10 @@ 'use strict'; | ||
var _createValidator = require('../createValidator'); | ||
var _internalMatchesPattern = require('../internal/validators/internalMatchesPattern'); | ||
var _createValidator2 = _interopRequireDefault(_createValidator); | ||
var _internalMatchesPattern2 = _interopRequireDefault(_internalMatchesPattern); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var regex = /^\d+$/; | ||
exports.default = (0, _createValidator2.default)(function (message) { | ||
return function (value) { | ||
if (value && !regex.test(value)) { | ||
return message; | ||
} | ||
}; | ||
}, function (field) { | ||
exports.default = (0, _internalMatchesPattern2.default)(/^\d+$/, function (field) { | ||
return field + ' must be numeric'; | ||
}); |
@@ -9,2 +9,6 @@ 'use strict'; | ||
var _valueMissing = require('../internal/valueMissing'); | ||
var _valueMissing2 = _interopRequireDefault(_valueMissing); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -14,9 +18,5 @@ | ||
return function (value) { | ||
if (value == null) { | ||
if ((0, _valueMissing2.default)(value)) { | ||
return message; | ||
} | ||
if (typeof value === 'string' && value.trim() === '') { | ||
return message; | ||
} | ||
}; | ||
@@ -23,0 +23,0 @@ }, function (field) { |
{ | ||
"name": "revalidate", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Composable form value validations", | ||
@@ -36,3 +36,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"ava": "^0.14.0", | ||
"ava": "^0.16.0", | ||
"babel-cli": "^6.9.0", | ||
@@ -50,8 +50,9 @@ "babel-core": "^6.9.0", | ||
"babel-plugin-transform-es2015-template-literals": "^6.8.0", | ||
"babel-plugin-transform-export-extensions": "^6.8.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.8.0", | ||
"babel-plugin-transform-runtime": "^6.9.0", | ||
"eslint": "^2.10.2", | ||
"eslint-config-airbnb-base": "^3.0.1", | ||
"eslint": "^3.2.2", | ||
"eslint-config-airbnb-base": "^5.0.1", | ||
"eslint-import-resolver-node": "^0.2.0", | ||
"eslint-plugin-import": "^1.8.0", | ||
"eslint-plugin-import": "^1.12.0", | ||
"lodash.range": "^3.1.5", | ||
@@ -102,3 +103,4 @@ "lodash.repeat": "^4.0.3", | ||
} | ||
] | ||
], | ||
"transform-export-extensions" | ||
] | ||
@@ -135,8 +137,18 @@ }, | ||
], | ||
"prefer-rest-params": 0 | ||
"prefer-rest-params": 0, | ||
"import/no-extraneous-dependencies": [ | ||
"error", | ||
{ | ||
"devDependencies": true | ||
} | ||
] | ||
} | ||
}, | ||
"ava": { | ||
"require": "babel-core/register" | ||
"require": "babel-core/register", | ||
"source": [ | ||
"**/*.js", | ||
"!**/*.swp" | ||
] | ||
} | ||
} |
@@ -1,27 +0,14 @@ | ||
import createValidator from './createValidator'; | ||
import composeValidators from './composeValidators'; | ||
import combineValidators from './combineValidators'; | ||
import hasLengthBetween from './validators/hasLengthBetween'; | ||
import hasLengthGreaterThan from './validators/hasLengthGreaterThan'; | ||
import hasLengthLessThan from './validators/hasLengthLessThan'; | ||
import isAlphabetic from './validators/isAlphabetic'; | ||
import isAlphaNumeric from './validators/isAlphaNumeric'; | ||
import isNumeric from './validators/isNumeric'; | ||
import isOneOf from './validators/isOneOf'; | ||
import isRequired from './validators/isRequired'; | ||
import matchesField from './validators/matchesField'; | ||
export { | ||
createValidator, | ||
composeValidators, | ||
combineValidators, | ||
hasLengthBetween, | ||
hasLengthGreaterThan, | ||
hasLengthLessThan, | ||
isAlphabetic, | ||
isAlphaNumeric, | ||
isNumeric, | ||
isOneOf, | ||
isRequired, | ||
matchesField, | ||
}; | ||
export createValidator from './createValidator'; | ||
export composeValidators from './composeValidators'; | ||
export combineValidators from './combineValidators'; | ||
export hasLengthBetween from './validators/hasLengthBetween'; | ||
export hasLengthGreaterThan from './validators/hasLengthGreaterThan'; | ||
export hasLengthLessThan from './validators/hasLengthLessThan'; | ||
export isAlphabetic from './validators/isAlphabetic'; | ||
export isAlphaNumeric from './validators/isAlphaNumeric'; | ||
export isNumeric from './validators/isNumeric'; | ||
export isOneOf from './validators/isOneOf'; | ||
export isRequired from './validators/isRequired'; | ||
export matchesField from './validators/matchesField'; | ||
export isRequiredIf from './validators/isRequiredIf'; | ||
export matchesPattern from './validators/matchesPattern'; |
import isValueValidator from './isValueValidator'; | ||
function buildErrorsArray(validators, validate) { | ||
return validators.reduce((errors, validator) => { | ||
const errorMessage = validate(validator); | ||
if (errorMessage) { | ||
errors.push(errorMessage); | ||
} | ||
return errors; | ||
}, []); | ||
} | ||
function buildErrorsObject(validators, validate) { | ||
return Object.keys(validators).reduce((errors, key) => { | ||
const validator = validators[key]; | ||
const errorMessage = validate(validator); | ||
if (errorMessage) { | ||
errors[key] = errorMessage; | ||
} | ||
return errors; | ||
}, {}); | ||
} | ||
export default function createValidatorWithMultipleErrors(validators, sharedConfig) { | ||
let buildErrors; | ||
let finalValidators; | ||
if (typeof validators[0] === 'object') { | ||
buildErrors = buildErrorsObject; | ||
finalValidators = validators[0]; | ||
} else { | ||
buildErrors = buildErrorsArray; | ||
finalValidators = validators; | ||
} | ||
return function composedValidator(value, allValues) { | ||
return validators.reduce((errors, validator) => { | ||
let errorMessage; | ||
return buildErrors(finalValidators, (validator) => { | ||
if (isValueValidator(validator)) { | ||
errorMessage = validator(value, allValues); | ||
} else { | ||
errorMessage = validator(sharedConfig, value, allValues); | ||
return validator(value, allValues); | ||
} | ||
if (errorMessage) { | ||
errors.push(errorMessage); | ||
} | ||
return errors; | ||
}, []); | ||
return validator(sharedConfig, value, allValues); | ||
}); | ||
}; | ||
} |
@@ -1,7 +0,2 @@ | ||
import fillObjectFromPath from './fillObjectFromPath'; | ||
import parseFieldName from './parseFieldName'; | ||
export { | ||
fillObjectFromPath, | ||
parseFieldName, | ||
}; | ||
export fillObjectFromPath from './fillObjectFromPath'; | ||
export parseFieldName from './parseFieldName'; |
@@ -0,3 +1,4 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import sym from './sym'; | ||
export const VALUE_VALIDATOR_SYMBOL = sym('VALUE_VALIDATOR'); |
@@ -1,13 +0,6 @@ | ||
import createValidator from '../createValidator'; | ||
import internalMatchesPattern from '../internal/validators/internalMatchesPattern'; | ||
const regex = /^[A-Za-z]+$/; | ||
export default createValidator( | ||
message => value => { | ||
if (value && !regex.test(value)) { | ||
return message; | ||
} | ||
}, | ||
export default internalMatchesPattern( | ||
/^[A-Za-z]+$/, | ||
field => `${field} must be alphabetic` | ||
); |
@@ -1,13 +0,6 @@ | ||
import createValidator from '../createValidator'; | ||
import internalMatchesPattern from '../internal/validators/internalMatchesPattern'; | ||
const regex = /^[0-9A-Za-z]+$/; | ||
export default createValidator( | ||
message => value => { | ||
if (value && !regex.test(value)) { | ||
return message; | ||
} | ||
}, | ||
export default internalMatchesPattern( | ||
/^[0-9A-Za-z]+$/, | ||
field => `${field} must be alphanumeric` | ||
); |
@@ -1,13 +0,6 @@ | ||
import createValidator from '../createValidator'; | ||
import internalMatchesPattern from '../internal/validators/internalMatchesPattern'; | ||
const regex = /^\d+$/; | ||
export default createValidator( | ||
message => value => { | ||
if (value && !regex.test(value)) { | ||
return message; | ||
} | ||
}, | ||
export default internalMatchesPattern( | ||
/^\d+$/, | ||
field => `${field} must be numeric` | ||
); |
import createValidator from '../createValidator'; | ||
import valueMissing from '../internal/valueMissing'; | ||
export default createValidator( | ||
message => value => { | ||
if (value == null) { | ||
if (valueMissing(value)) { | ||
return message; | ||
} | ||
if (typeof value === 'string' && value.trim() === '') { | ||
return message; | ||
} | ||
}, | ||
@@ -13,0 +10,0 @@ |
48234
61
828
23