Socket
Socket
Sign inDemoInstall

entity-validator

Package Overview
Dependencies
1
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    entity-validator

Simple object validator


Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Install size
393 kB
Created
Weekly downloads
 

Readme

Source

Dead simple sync/async object validation library with focus on custom matchers.

Install

npm i entity-validator

How to use?

validate(params)

params.entity: Entity params.matchers: Array, params.attributes: Array,

You have entity for validate

const entity = {};

and attributes rules

const attributes = [
  {
    key: 'name',
    rules: [ 'presence' ], // or [ { key: 'presence', params: {} } ]
  },
];

and matchers which used in rules

const presenceMatcher = {
  key: 'presence',
  check: (entity, attribute) => {
    if (!attribute || !entity || entity[attribute]) return null;
    return { messageKey: 'presenceText' };
  },
};

const matchers = [ presenceMatcher ];

now you can get validation matchers errors!

const { validate } = require('entity-validator');
const { matchersErrors } = await validate({ entity, attributes, matchers });

// > matchersErrors
// [
//   {
//     key: 'presence',
//     attribute: 'name',
//     checkResult: { messageKey: 'presenceText' },
//   },
// ]

Helpers

createAttributesByValidator(validator, params)

Tranform validator object (groupped attributes) to attributes.

  const { createAttributesByValidator } = require('entity-valdiator');

  const validator = {
    key: 'user',
    attributes: ({ passwordMinLength, passwordMaxLength }) => ([
      {
        key: 'email',
        rules: [
          'presence',
          ['length', { min: passwordMinLength, max: passwordMaxLength }],
        ]
      },
    ])
  };

  const params = { passwordMinLength: 6, passwordMaxLength: 12 };
  const attributes = createAttributesByValidator(validator, params);

  // attributes => [
  //   {
  //     key: 'email',
  //     rules: [
  //       'presence',
  //       ['length', { min: 6, max: 12 }],
  //     ]
  //   },
  // ]);

Flow types

  import type { Validator, /* ... */ } = from('entity-valdiator/build/types');

Keywords

FAQs

Last updated on 18 Apr 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc