calidation
Advanced tools
+14
-6
@@ -43,5 +43,11 @@ 'use strict'; | ||
| return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Validation.__proto__ || Object.getPrototypeOf(Validation)).call.apply(_ref, [this].concat(args))), _this), _this.getInitialFieldValues = function () { | ||
| return Object.keys(_this.props.config).reduce(function (fields, field) { | ||
| return _extends({}, fields, _defineProperty({}, field, _this.props.initialValues[field] || '')); | ||
| return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Validation.__proto__ || Object.getPrototypeOf(Validation)).call.apply(_ref, [this].concat(args))), _this), _this.getFieldValues = function () { | ||
| var _this$props = _this.props, | ||
| config = _this$props.config, | ||
| fields = _this$props.fields, | ||
| initialValues = _this$props.initialValues; | ||
| return Object.keys(config).reduce(function (allFields, field) { | ||
| return _extends({}, allFields, _defineProperty({}, field, fields[field] || initialValues[field] || '')); | ||
| }, {}); | ||
@@ -54,3 +60,3 @@ }, _temp), _possibleConstructorReturn(_this, _ret); | ||
| value: function componentDidMount() { | ||
| this.props.register(this.props.config, this.getInitialFieldValues()); | ||
| this.props.register(this.props.config, this.getFieldValues()); | ||
| } | ||
@@ -69,3 +75,4 @@ }, { | ||
| submitted = _props.submitted, | ||
| children = _props.children; | ||
| children = _props.children, | ||
| config = _props.config; | ||
@@ -75,5 +82,6 @@ | ||
| errors: errors, | ||
| fields: fields, | ||
| fields: this.getFieldValues(), | ||
| submitted: submitted | ||
| }; | ||
| return children(childrenArgs); | ||
@@ -80,0 +88,0 @@ } |
@@ -18,6 +18,4 @@ 'use strict'; | ||
| var _validators = require('./validators'); | ||
| var _calidators = require('calidators'); | ||
| var _validators2 = _interopRequireDefault(_validators); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -31,2 +29,14 @@ | ||
| var defaultValidators = { | ||
| isBlacklisted: _calidators.isBlacklisted, | ||
| isEmail: _calidators.isEmail, | ||
| isEqual: _calidators.isEqual, | ||
| isGreaterThan: _calidators.isGreaterThan, | ||
| isLessThan: _calidators.isLessThan, | ||
| isRequired: _calidators.isRequired, | ||
| isNumber: _calidators.isNumber, | ||
| isRegexMatch: _calidators.isRegexMatch, | ||
| isWhitelisted: _calidators.isWhitelisted | ||
| }; | ||
| var _createContext = (0, _react.createContext)({}), | ||
@@ -87,3 +97,3 @@ Provider = _createContext.Provider, | ||
| return _react2.default.createElement(TargetComponent, _extends({}, props, { | ||
| validators: _extends({}, _validators2.default, validators) | ||
| validators: _extends({}, defaultValidators, validators) | ||
| })); | ||
@@ -90,0 +100,0 @@ } |
+12
-3
| { | ||
| "name": "calidation", | ||
| "version": "1.1.1", | ||
| "version": "1.2.0", | ||
| "description": "A red hot validation library for React", | ||
@@ -22,3 +22,4 @@ "main": "dist/index.js", | ||
| "test": "jest", | ||
| "test:watch": "jest --watch", | ||
| "test:watch": "jest --watch --coverage", | ||
| "test:coverage": "jest --coverage", | ||
| "semantic-release": "semantic-release", | ||
@@ -35,2 +36,3 @@ "travis-deploy-once": "travis-deploy-once" | ||
| "cz-conventional-changelog": "^2.1.0", | ||
| "dom-testing-library": "^1.1.0", | ||
| "husky": "^0.14.3", | ||
@@ -42,2 +44,3 @@ "jest": "^22.4.3", | ||
| "react-dom": "^16.3.1", | ||
| "react-testing-library": "^2.0.0", | ||
| "rimraf": "^2.6.2", | ||
@@ -61,4 +64,10 @@ "semantic-release": "^15.1.5", | ||
| "dependencies": { | ||
| "invariant": "^2.2.4" | ||
| "calidators": "^1.0.0", | ||
| "invariant": "^2.2.4", | ||
| "prop-types": "^15.6.1" | ||
| }, | ||
| "jest": { | ||
| "coverageDirectory": "./coverage/", | ||
| "collectCoverage": true | ||
| } | ||
| } |
+44
-13
@@ -5,2 +5,4 @@ # calidation | ||
| [](https://travis-ci.org/selbekk/calidation) [](https://codecov.io/gh/selbekk/calidation) | ||
| ``` | ||
@@ -40,3 +42,3 @@ yarn add calidation | ||
| password: { | ||
| isRequired: 'Password is required!', | ||
| isRequired: 'Password is also required!', | ||
| }, | ||
@@ -48,3 +50,3 @@ }; | ||
| You can add as many validators as you want, and they'll be run from top to | ||
| bottom. | ||
| bottom. For more about validators, go to the [validators](#validators) section! | ||
@@ -58,14 +60,14 @@ ### Validate that form | ||
| const config = {...}; // We did this above! | ||
| const config = {...}; // See above | ||
| const MyForm = props => ( | ||
| <FormValidation onSubmit={props.onSubmit} config={config}> | ||
| {({ fields, errors }) => ( | ||
| {({ fields, errors, submitted }) => ( | ||
| <> | ||
| <label> | ||
| Username: <input name="username" value={fields.username} /> | ||
| {errors.username && <span>{errors.username}</span>} | ||
| {submitted && errors.username && <span>{errors.username}</span>} | ||
| </label> | ||
| <label> | ||
| Password: <input name="password" value={fields.password} /> | ||
| {errors.password && <span>{errors.password}</span>} | ||
| {submitted && errors.password && <span>{errors.password}</span>} | ||
| </label> | ||
@@ -128,6 +130,6 @@ <button>Log in</button> | ||
| {/* ...tons of other components and other stuff */} | ||
| <Validation config={props.config}> | ||
| <Validation config={props.anotherConfig}> | ||
| {({ fields, errors }) => ( | ||
| <> | ||
| And what does he do? | ||
| What does he do? | ||
| <input name="dadWork" value={fields.dadWork} /> | ||
@@ -142,4 +144,11 @@ </> | ||
| The `onSubmit` handler will receive a merged object of all the validated fields | ||
| below it, as well as a merged object of all the errors. | ||
| below it, as well as a merged object of all the errors: | ||
| ```js | ||
| const onSubmit = ({ fields, errors, isValid }) => { | ||
| // fields and errors now contain both `daddy` and `dadWork` | ||
| // isValid is true if all forms are valid, otherwise false. | ||
| }; | ||
| ``` | ||
| ## Validators | ||
@@ -264,8 +273,30 @@ | ||
| ### Deluxe validators | ||
| #### `isWhitelisted` | ||
| I haven't implemented any yet, but in the future I imagine you can import some | ||
| validators that aren't used that often from `calidation/validators`. But that's | ||
| a future issue to adress. | ||
| Validates that a field is present in a provided whitelist. The whitelist must be | ||
| an array. | ||
| ```js | ||
| firstName: { | ||
| isWhitelisted : { | ||
| message: 'Bros only, bro', | ||
| whitelist: ['Chad', 'Bret'], | ||
| }, | ||
| }, | ||
| ``` | ||
| #### `isBlacklisted` | ||
| Validates that a field is not present in a provided blacklist. The blacklist | ||
| must be an array. | ||
| ```js | ||
| firstName: { | ||
| isBlacklisted : { | ||
| message: 'Bros are not welcome', | ||
| blacklist: ['Chad', 'Bret'], | ||
| }, | ||
| }, | ||
| ``` | ||
| ### Custom validators | ||
@@ -272,0 +303,0 @@ |
| 'use strict'; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| var _isRequired = require('./isRequired'); | ||
| var _isRequired2 = _interopRequireDefault(_isRequired); | ||
| var _isNumber = require('./isNumber'); | ||
| var _isNumber2 = _interopRequireDefault(_isNumber); | ||
| var _isEqual = require('./isEqual'); | ||
| var _isEqual2 = _interopRequireDefault(_isEqual); | ||
| var _isGreaterThan = require('./isGreaterThan'); | ||
| var _isGreaterThan2 = _interopRequireDefault(_isGreaterThan); | ||
| var _isLessThan = require('./isLessThan'); | ||
| var _isLessThan2 = _interopRequireDefault(_isLessThan); | ||
| var _isEmail = require('./isEmail'); | ||
| var _isEmail2 = _interopRequireDefault(_isEmail); | ||
| var _isRegexMatch = require('./isRegexMatch'); | ||
| var _isRegexMatch2 = _interopRequireDefault(_isRegexMatch); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
| exports.default = { | ||
| isRequired: _isRequired2.default, | ||
| isNumber: _isNumber2.default, | ||
| isEqual: _isEqual2.default, | ||
| isGreaterThan: _isGreaterThan2.default, | ||
| isLessThan: _isLessThan2.default, | ||
| isEmail: _isEmail2.default, | ||
| isRegexMatch: _isRegexMatch2.default | ||
| }; |
| 'use strict'; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| var EMAIL_REGEXP = /^\S+@\S+$/; | ||
| exports.default = function (config) { | ||
| return function (value) { | ||
| if (value === '') { | ||
| return null; | ||
| } | ||
| return !EMAIL_REGEXP.test(value) ? config.message : null; | ||
| }; | ||
| }; |
| 'use strict'; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
| exports.default = function (config) { | ||
| return function (value) { | ||
| if (value === '') { | ||
| return null; | ||
| } | ||
| var isValid = void 0; | ||
| switch (_typeof(config.value)) { | ||
| case 'number': | ||
| { | ||
| isValid = Number(value) === config.value; | ||
| break; | ||
| } | ||
| case 'boolean': | ||
| { | ||
| isValid = config.value ? value === 'true' : value === 'false'; | ||
| break; | ||
| } | ||
| case 'string': | ||
| default: | ||
| { | ||
| isValid = String(value) === config.value; | ||
| } | ||
| } | ||
| return isValid ? null : config.message; | ||
| }; | ||
| }; |
| 'use strict'; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.default = function (config) { | ||
| return function (value) { | ||
| if (value === '') { | ||
| return null; | ||
| } | ||
| return value <= config.value ? config.message : null; | ||
| }; | ||
| }; |
| 'use strict'; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.default = function (config) { | ||
| return function (value) { | ||
| if (value === '') { | ||
| return null; | ||
| } | ||
| return value >= config.value ? config.message : null; | ||
| }; | ||
| }; |
| 'use strict'; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.default = function (config) { | ||
| return function (value) { | ||
| if (value === '') { | ||
| return null; | ||
| } | ||
| return isNaN(value) ? config.message : null; | ||
| }; | ||
| }; |
| 'use strict'; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.default = function (config) { | ||
| return function (value) { | ||
| if (value === '') { | ||
| return null; | ||
| } | ||
| return !config.regex.test(value) ? config.message : null; | ||
| }; | ||
| }; |
| 'use strict'; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.default = function (config) { | ||
| return function (value) { | ||
| return value === '' ? config.message : null; | ||
| }; | ||
| }; |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
323
10.62%31892
-6.56%5
66.67%18
12.5%8
-50%380
-21.81%+ Added
+ Added
+ Added