Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cerebral-forms

Package Overview
Dependencies
Maintainers
2
Versions
186
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cerebral-forms - npm Package Compare versions

Comparing version 1.0.0-b-alpha.b2be293f to 1.0.0-b-alpha.b2f2b666

7

lib/chains/changeField.js

@@ -9,6 +9,9 @@ 'use strict';

_templateObject2 = _taggedTemplateLiteral(['field'], ['field']),
_templateObject3 = _taggedTemplateLiteral(['value'], ['value']);
_templateObject3 = _taggedTemplateLiteral(['value'], ['value']),
_templateObject4 = _taggedTemplateLiteral(['', ''], ['', '']);
var _operators = require('cerebral/operators');
var _tags = require('cerebral/tags');
var _validateField = require('../factories/validateField');

@@ -22,3 +25,3 @@

exports.default = [(0, _operators.set)((0, _operators.state)(_templateObject, (0, _operators.input)(_templateObject2)), (0, _operators.input)(_templateObject3)), (0, _validateField2.default)((0, _operators.input)(_templateObject2))];
exports.default = [(0, _operators.set)((0, _tags.state)(_templateObject, (0, _tags.input)(_templateObject2)), (0, _tags.input)(_templateObject3)), (0, _validateField2.default)((0, _tags.state)(_templateObject4, (0, _tags.input)(_templateObject2)))];
//# sourceMappingURL=changeField.js.map

@@ -13,12 +13,31 @@ 'use strict';

function isValidFormFactory(formPathTemplate) {
function isValidForm(context) {
var formPath = typeof formPathTemplate === 'function' ? formPathTemplate(context).value : formPathTemplate;
var form = context.state.get(formPath);
function isValidFormFactory(formPath) {
function isValidForm(_ref) {
var state = _ref.state,
path = _ref.path,
resolveArg = _ref.resolveArg;
if ((0, _isValidForm2.default)(form)) {
return context.path.true();
if (typeof formPath === 'string') {
console.warn('DEPRECATION: Cerebral Forms now requires STATE TAG to be passed into isValidForm factory');
var form = state.get(formPath);
if ((0, _isValidForm2.default)(form)) {
return path.true();
}
return path.false();
} else {
if (!resolveArg.isTag(formPath, 'state')) {
throw new Error('Cerebral Forms - isValidForm factory requires a STATE TAG');
}
var _form = resolveArg.value(formPath);
if ((0, _isValidForm2.default)(_form)) {
return path.true();
}
return path.false();
}
return context.path.false();
}

@@ -25,0 +44,0 @@

@@ -42,8 +42,22 @@ 'use strict';

function resetFormFactory(formPathTemplate) {
function resetForm(context) {
var formPath = typeof formPathTemplate === 'function' ? formPathTemplate(context).value : formPathTemplate;
var form = context.state.get(formPath);
function resetFormFactory(formPath) {
function resetForm(_ref) {
var state = _ref.state,
resolveArg = _ref.resolveArg;
context.state.merge(formPath, resetObject(form));
if (typeof formPath === 'string') {
console.warn('DEPRECATION: Cerebral Forms now requires STATE TAG to be passed into resetForm factory');
var form = state.get(formPath);
state.merge(formPath, resetObject(form));
} else {
if (!resolveArg.isTag(formPath, 'state')) {
throw new Error('Cerebral Forms - isValidForm factory requires a STATE TAG');
}
var _form = resolveArg.value(formPath);
state.merge(resolveArg.path(formPath), resetObject(_form));
}
}

@@ -50,0 +64,0 @@

@@ -14,9 +14,24 @@ 'use strict';

function validateFieldFactory(pathTemplate) {
function validateField(context) {
var path = typeof pathTemplate === 'function' ? pathTemplate(context).value : pathTemplate;
var fieldPath = path.split('.');
var formPath = fieldPath.slice().splice(0, fieldPath.length - 1);
var field = context.state.get(fieldPath);
var form = context.state.get(formPath);
function validateFieldFactory(fieldPath) {
function validateField(_ref) {
var state = _ref.state,
resolveArg = _ref.resolveArg;
var path = void 0;
if (typeof fieldPath === 'string') {
console.warn('DEPRECATION: Cerebral Forms now requires STATE TAG to be passed into validateField factory');
path = fieldPath;
} else {
if (!resolveArg.isTag(fieldPath, 'state')) {
throw new Error('Cerebral Forms - validateField factory requires a STATE TAG');
}
path = resolveArg.path(fieldPath);
}
var fieldPathAsArray = path.split('.');
var formPath = fieldPathAsArray.slice().splice(0, fieldPathAsArray.length - 1);
var field = state.get(path);
var form = state.get(formPath);
var validationResult = (0, _runValidation2.default)(field, form);

@@ -34,4 +49,4 @@

var dependentFormPath = dependentFieldPath.slice().splice(0, dependentFieldPath.length - 1);
var dependentField = context.state.get(dependentFieldPath);
var dependentForm = context.state.get(dependentFormPath);
var dependentField = state.get(dependentFieldPath);
var dependentForm = state.get(dependentFormPath);
if (!dependentForm || !dependentField) {

@@ -42,3 +57,3 @@ throw new Error('The path ' + stringPath + ' used with "dependsOn" on field ' + fieldPath.join('.') + ' is not correct, please check it');

var dependentValidationResult = (0, _runValidation2.default)(dependentField, dependentForm);
context.state.merge(dependentFieldPath, dependentValidationResult);
state.merge(dependentFieldPath, dependentValidationResult);

@@ -52,3 +67,3 @@ if (currentValidationResult.isValid && !dependentValidationResult.isValid) {

context.state.merge(fieldPath, dependentOfValidationResult);
state.merge(path, dependentOfValidationResult);
}

@@ -55,0 +70,0 @@

@@ -14,9 +14,9 @@ 'use strict';

function validateFormFactory(passedFormPathTemplate) {
function validateForm(context) {
var passedFormPath = typeof passedFormPathTemplate === 'function' ? passedFormPathTemplate(context).value : passedFormPathTemplate;
var formPath = passedFormPath.split('.');
var currentPathValue = context.state.get(formPath);
function validateFormFactory(formPath) {
function validateForm(_ref) {
var state = _ref.state,
input = _ref.input,
resolveArg = _ref.resolveArg;
function validateForm(path, form) {
function validate(path, form) {
Object.keys(form).forEach(function (key) {

@@ -27,5 +27,5 @@ if (form[key] === Object(form[key])) {

} else if ('value' in form[key]) {
context.state.merge(path.concat(key), (0, _runValidation2.default)(form[key], form));
state.merge(path.concat(key), (0, _runValidation2.default)(form[key], form));
} else {
validateForm(path.concat(key), form[key]);
validate(path.concat(key), form[key]);
}

@@ -38,7 +38,15 @@ }

formArray.forEach(function (form, index) {
validateForm(path.concat(index), form);
validate(path.concat(index), form);
});
}
if (typeof formPath === 'string') {
console.warn('DEPRECATION: Cerebral Forms now requires STATE TAG to be passed into validateForm factory');
validate(formPath.split('.'), state.get(formPath));
} else {
if (!resolveArg.isTag(formPath, 'state')) {
throw new Error('Cerebral Forms - validateField factory requires a STATE TAG');
}
validateForm(formPath, currentPathValue);
validate(resolveArg.path(formPath).split('.'), resolveArg.value(formPath));
}
}

@@ -45,0 +53,0 @@

@@ -19,12 +19,7 @@ 'use strict';

});
currentPath.pop();
return allFields;
} else if (object[key] === Object(object[key]) && 'value' in object[key]) {
allFields[currentPath.join('.')] = object[key];
currentPath.pop();
return allFields;
} else if (object[key] === Object(object[key])) {
getFormFields(object[key], currentPath, allFields);
}
getFormFields(object[key], currentPath, allFields);
currentPath.pop();

@@ -31,0 +26,0 @@

@@ -90,2 +90,4 @@ 'use strict';

var _tags = require('cerebral/tags');
var _isValidForm = require('./helpers/isValidForm');

@@ -102,3 +104,3 @@

function isValidForm(form) {
if (typeof form === 'string' || typeof form === 'function') {
if (typeof form === 'string' || form instanceof _tags.Tag) {
return (0, _isValidForm4.default)(form);

@@ -105,0 +107,0 @@ }

{
"name": "cerebral-forms",
"version": "1.0.0-b-alpha.b2be293f",
"version": "1.0.0-b-alpha.b2f2b666",
"description": "Signals, actions and state factories to create forms",
"main": "lib/index.js",
"scripts": {
"test": "../../node_modules/.bin/mocha --compilers js:../../node_modules/babel-register 'src/**/*.test.js'",
"test": "mocha --compilers js:babel-register 'src/**/*.test.js'",
"test:watch": "npm run test -- --watch",
"prebuild": "npm run test",
"build": "BABEL_ENV=production ../../node_modules/.bin/babel src/ --out-dir=lib/ -s",
"coverage": "../../node_modules/.bin/nyc --reporter=lcov --reporter=json npm run test",
"build": "BABEL_ENV=production babel src/ --out-dir=lib/ -s",
"coverage": "nyc --reporter=lcov --reporter=json npm run test",
"prepublish": "npm run build"

@@ -29,6 +28,7 @@ },

"peerDependencies": {
"cerebral": "^2.0.0-b-alpha.b2be293f"
"cerebral": "^2.0.0-b-alpha.b2f2b666"
},
"devDependencies": {
"cerebral": "^2.0.0-b-alpha.b2be293f"
"@cerebral/monorepo": "^0.0.1-alpha.b2f2b666",
"cerebral": "^2.0.0-b-alpha.b2f2b666"
},

@@ -35,0 +35,0 @@ "nyc": {

@@ -50,6 +50,4 @@ # cerebral-forms

// Some properties are created for you, set when validation runs
// and you can use them in components. You can also set these
// properties manually, though usually the validate action factory
// is used to handle this
// Some properties are created for you by the validate action factory
// which can be used in the components.

@@ -97,2 +95,22 @@ // Toggled when field is validated

#### Custom global props
You can add custom props to the root to the form state.
For example if you want to show validation errors only
when submitting the form you can add a `showErrors` prop
which you set true when validation fails during form submit.
```js
import {form} from 'cerebral-forms'
export default function MyAction({state}) {
state.set('some.new.form', form({
name: {
value: '',
},
showErrors: false
}))
}
```
#### Set a default value for the whole form

@@ -108,3 +126,3 @@ You can set a default value for a property using a factory:

// You can also set some special properties for the whole form
newForm.showErrors = false
myForm.showErrors = false

@@ -121,22 +139,2 @@ fields.forEach((field) => {

#### Custom global props
You can add custom props to the root to the form state.
For example if you want to show validation errors only
when submitting the form you can add a `showErrors` prop
which you set true when validation fails during form submit.
```js
import {form} from 'cerebral-forms'
export default function MyAction({state}) {
state.set('some.new.form', form({
name: {
value: '',
},
showErrors: false
}))
}
```
### field

@@ -143,0 +141,0 @@ To add a new field you simply merge a new form into the existing one.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc