New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cicero-form-validator

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cicero-form-validator

A form validator that takes a JSON based configuration.

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

Validator

A form validator that takes a JSON based configuration.

Install

npm i --save cicero-form-validator

Usage

import validator, {multipleValidator} from 'cicero-form-validator'

const validate = validator({
  field: { required: true }
})

validate(['field'], {})
// => { field: { field: 'field', rule: 'required', value: undefined } }


const validateMultiple = multipleValidator({
  field: { required: true }
})


const validateMultiple = multipleValidator({ field: { required: true } })

validateMultiple(['field'], { 1: {} })
// => { 1: { field: { field: 'field', rule: 'required', value: undefined } } }

Configuration

Options that work for all rules
if

Should be a function which returns a boolean. It will be called with an object containing all the fields as a first argument.

The example below will make field required if otherField is filled with "100".

{
  field: {
    required: { if: (fields) => fields.otherField === "100" }
  }
}

Adding new rules

import {extend} from 'cicero-form-validator'

const {validator, multipleValidator} = extend({
  mod: (field, value, options) => value % options.constant === 0 ? null : 'mod'
});

validator({ f: { mod: { constant: 2 } })(['f'], { f: 3 })
// => { f: { field: 'f', rule: 'mod', value: 3 } }

multipleValidator({ f: { mod: { constant: 2 } } })(['f'], { 1: { f: 3 } })
// => { '1': { f: { field: 'f', rule: 'mod', value: 3, config: [Object] } } }

Creating rules

A rule is a function that takes three arguments: field, value and options. It then validates the value based on a given conditions and returns null if the value validates. If not it should return the name of the rule that did not validate. A rule can have sub-rules (e.g. numeric validation with max value). It should then return 'rule/sub-rule' if the data does not validate.

FAQs

Package last updated on 28 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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