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

rattata

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rattata

Simple data validation for JavaScript

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Rattata (WIP)

Simple data validation for JavaScript

Example

const rules = {
  firstName: r.withMessage(r.string, 'should be a string'),
  middleName: r.opt(r.string),
  lastName: r.string,
  age: r.xor(
    r.undefined,
    r.and(
      r.withMessage(r.integer, 'should be an integer'),
      r.withMessage(r.range(10, 99), 'should be between 10 and 99'),
    ),
  ),
  pokedex: r.validate({
    serialNumber: r.pattern(/\d\d\d\d-\d\d\d\d/),
    pokemonIds: r.array(
      withMessage(
        r.and(
          r.integer,
          r.range(1, 151)
      ), 'should be an integer between 1 and 151')
    ),
  }),
}

const props = {
  firstName: 42,
  // middleName is optional
  lastName: [],
  age: '100',
  pokedex: {
    serialNumber: '1234-5678',
    pokemonIds: [23, 1, 56, '11'],
  },
}

r
  .validate(rules, props)
  .then(() => {
    // validation successful
  })
  .catch(err => {
    // err {
    //   firstName: ['should be a string'],
    //   lastName: [],
    //   age: ['should be an integer', 'should be between 10 and 99'],
    //   pokedex: {
    //     'pokemonIds[i]': ['should be an integer between 1 and 151'],
    //   }
    // }
  })

Keywords

FAQs

Package last updated on 13 Feb 2018

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