redux-form-schema
Advanced tools
Comparing version 0.0.3 to 1.0.0-beta
@@ -6,27 +6,36 @@ 'use strict'; | ||
}); | ||
exports.buildValidationFn = undefined; | ||
var _validator = require('validator'); | ||
var _each = require('lodash/each'); | ||
var _validator2 = _interopRequireDefault(_validator); | ||
var _each = require('lodash/collection/each'); | ||
var _each2 = _interopRequireDefault(_each); | ||
var _startCase = require('lodash/string/startCase'); | ||
var _get = require('lodash/get'); | ||
var _startCase2 = _interopRequireDefault(_startCase); | ||
var _get2 = _interopRequireDefault(_get); | ||
var _isFunction = require('lodash/lang/isFunction'); | ||
var _isFunction = require('lodash/isFunction'); | ||
var _isFunction2 = _interopRequireDefault(_isFunction); | ||
var _isUndefined = require('lodash/lang/isUndefined'); | ||
var _isUndefined = require('lodash/isUndefined'); | ||
var _isUndefined2 = _interopRequireDefault(_isUndefined); | ||
var _isString = require('lodash/lang/isString'); | ||
var _isString = require('lodash/isString'); | ||
var _isString2 = _interopRequireDefault(_isString); | ||
var _set = require('lodash/set'); | ||
var _set2 = _interopRequireDefault(_set); | ||
var _startCase = require('lodash/startCase'); | ||
var _startCase2 = _interopRequireDefault(_startCase); | ||
var _validator = require('validator'); | ||
var _validator2 = _interopRequireDefault(_validator); | ||
var _errorMessages = require('./error-messages'); | ||
@@ -38,12 +47,10 @@ | ||
exports.default = function (schema) { | ||
var fields = Object.keys(schema); | ||
var validate = buildValidationFn(schema); | ||
return { | ||
fields: fields, | ||
validate: validate | ||
}; | ||
var buildValidationFn = function buildValidationFn(schema) { | ||
return buildValidateFn(schema); | ||
}; | ||
function buildValidationFn(schema) { | ||
exports.buildValidationFn = buildValidationFn; | ||
function buildValidateFn(schema) { | ||
return function (formValues) { | ||
@@ -58,10 +65,10 @@ var errors = {}; | ||
(0, _each2.default)(schema, function (definition, fieldRef) { | ||
var label = definition.label; | ||
var required = definition.required; | ||
var type = definition.type; | ||
var validate = definition.validate; | ||
var error = definition.error; | ||
var label = definition.label, | ||
required = definition.required, | ||
type = definition.type, | ||
validate = definition.validate, | ||
error = definition.error; | ||
var fieldValue = formValues[fieldRef]; | ||
var fieldValueExists = isDefined(formValues[fieldRef]); | ||
var fieldValue = (0, _get2.default)(formValues, fieldRef); | ||
var fieldValueExists = isDefined(fieldValue); | ||
@@ -101,3 +108,3 @@ // required is active if it is `true` or a function that returns | ||
var isValid = undefined; | ||
var isValid = void 0; | ||
var customValidator = (0, _isFunction2.default)(opts) && opts; | ||
@@ -113,4 +120,4 @@ | ||
// use custom error message or fallback to default | ||
var message = error || (0, _errorMessages2.default)(id, label, opts); | ||
addErrorToField(errors, fieldRef, message); | ||
var _message = error || (0, _errorMessages2.default)(id, label, opts); | ||
addErrorToField(errors, fieldRef, _message); | ||
} | ||
@@ -126,4 +133,7 @@ }); | ||
function addErrorToField(errors, fieldRef, errorMessage) { | ||
errors[fieldRef] = errors[fieldRef] || []; | ||
errors[fieldRef].push(errorMessage); | ||
var field = (0, _get2.default)(errors, fieldRef); | ||
if (field) return field.push(errorMessage); | ||
(0, _set2.default)(errors, fieldRef, [errorMessage]); | ||
} | ||
@@ -130,0 +140,0 @@ |
{ | ||
"name": "redux-form-schema", | ||
"version": "0.0.3", | ||
"description": "Extension to Redux Form allowing creation of schemas (with valididation) to manage your form fields", | ||
"version": "1.0.0-beta", | ||
"description": "Extension to Redux Form to create validation function using passed schema to help manage form fields", | ||
"main": "build/index.js", | ||
"scripts": { | ||
"clean": "rm -r -f build/*", | ||
"release": "npm run clean && babel lib --out-dir build", | ||
"test": "mocha --compilers js:babel-core/register --reporter Spec --grep ${grep:-''} lib/**/*.test.js", | ||
"test:watch": "npm test -- --watch" | ||
"build": "yarn run clean && node_modules/.bin/babel src --ignore *.test.js --out-dir build", | ||
"clean": "node_modules/.bin/rimraf build", | ||
"lint": "node_modules/.bin/eslint src", | ||
"prepublish": "yarn build", | ||
"preversion": "yarn test", | ||
"test": "./node_modules/.bin/mocha", | ||
"test:ci": "yarn lint && yarn test:coverage -- --reporter mocha-junit-reporter", | ||
"test:coverage": "./node_modules/.bin/nyc ./node_modules/.bin/mocha", | ||
"version": "yarn build" | ||
}, | ||
"babel": { | ||
"presets": ["es2015"] | ||
}, | ||
"author": "https://github.com/willmcclellan", | ||
@@ -20,11 +22,24 @@ "license": "MIT", | ||
"babel-core": "^6.0.20", | ||
"babel-eslint": "^4.1.3", | ||
"babel-eslint": "7.1.1", | ||
"babel-preset-es2015": "^6.0.15", | ||
"eslint": "^1.8.0", | ||
"mocha": "^2.3.3" | ||
"eslint": "3.17.1", | ||
"lodash.contains": "^2.4.3", | ||
"lodash.includes": "^4.3.0", | ||
"lodash.omit": "^4.5.0", | ||
"mocha": "3.2.0", | ||
"mocha-junit-reporter": "^1.13.0", | ||
"nyc": "^10.1.2", | ||
"rimraf": "^2.6.1", | ||
"should": "^11.2.1" | ||
}, | ||
"dependencies": { | ||
"lodash": "^3.10.1", | ||
"lodash.foreach": "^4.5.0", | ||
"lodash.get": "^4.4.2", | ||
"lodash.isfunction": "^3.0.8", | ||
"lodash.isstring": "^4.0.1", | ||
"lodash.isundefined": "^3.0.1", | ||
"lodash.set": "^4.3.2", | ||
"lodash.startcase": "^4.4.0", | ||
"validator": "^4.2.1" | ||
} | ||
} |
@@ -14,9 +14,9 @@ redux-form-schema | ||
``` | ||
```javascript | ||
// schema.js | ||
import buildSchema from 'redux-form-schema' | ||
import buildValidateFn from 'redux-form-schema' | ||
const schema = { | ||
'name': { | ||
export const schema = { | ||
name: { | ||
label: 'Name', | ||
@@ -29,3 +29,3 @@ required: true, | ||
}, | ||
'city': { | ||
city: { | ||
label: 'City', | ||
@@ -51,16 +51,24 @@ // conditional required validation | ||
export default buildSchema(schema) | ||
export const validateFn = buildValidateFn(schema) | ||
``` | ||
``` | ||
```javascript | ||
// component.js (using redux-form) | ||
import { component } from 'react' | ||
import { connectReduxForm } from 'redux-form' | ||
import { fields, validate } from './schema' | ||
import { Field, reduxForm } from 'redux-form' | ||
import { schema, validateFn } from './schema' | ||
@connectReduxForm({ | ||
const renderInput = field => | ||
<div> | ||
<label htmlFor={field.input.name}>field.label</label> | ||
<input {...field.input} type={field.type}/> | ||
{field.meta.touched && | ||
field.meta.error && | ||
<span className="error">{field.meta.error}</span>} | ||
</div> | ||
@reduxForm({ | ||
form: 'myForm', | ||
fields: fields, | ||
validate: validate | ||
validate: validateFn | ||
} | ||
@@ -71,3 +79,2 @@ export default class FormComponent extends Component { | ||
handleSubmit: PropTypes.func.isRequired, | ||
fields: PropTypes.obj.isRequired, | ||
validate: PropTypes.func.isRequired, | ||
@@ -77,11 +84,11 @@ } | ||
render() { | ||
const { fields, handleSubmit, error } = this.props | ||
const { handleSubmit, error } = this.props | ||
return ( | ||
<form onSubmit={handleSubmit()} noValidate> | ||
<input type='text' {...fields.name} /> | ||
<input type='email' {...fields.email} /> | ||
<input type='text' {...fields['street-address']} /> | ||
<input type='text' {...fields.city} /> | ||
</form> | ||
<form onSubmit={handleSubmit()} noValidate> | ||
<Field {...schema.name} component={renderInput} /> | ||
<Field {...schema['street-address']} component={renderInput} /> | ||
<Field {...schema.city} component={renderInput} /> | ||
<Field {...schema['date-of-birth']} component={renderInput} /> | ||
</form> | ||
) | ||
@@ -127,3 +134,3 @@ } | ||
``` | ||
```javascript | ||
validate: { | ||
@@ -148,3 +155,3 @@ // validate value is an integer that is a minimum of 0 and a maximum of 100 | ||
``` | ||
```javascript | ||
{ | ||
@@ -151,0 +158,0 @@ name: { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
142376
13
174
8
13
180
2
+ Addedlodash.foreach@^4.5.0
+ Addedlodash.get@^4.4.2
+ Addedlodash.isfunction@^3.0.8
+ Addedlodash.isstring@^4.0.1
+ Addedlodash.isundefined@^3.0.1
+ Addedlodash.set@^4.3.2
+ Addedlodash.startcase@^4.4.0
+ Addedlodash.foreach@4.5.0(transitive)
+ Addedlodash.get@4.4.2(transitive)
+ Addedlodash.isfunction@3.0.9(transitive)
+ Addedlodash.isstring@4.0.1(transitive)
+ Addedlodash.isundefined@3.0.1(transitive)
+ Addedlodash.set@4.3.2(transitive)
+ Addedlodash.startcase@4.4.0(transitive)
- Removedlodash@^3.10.1
- Removedlodash@3.10.1(transitive)