Giter Site home page Giter Site logo

shape's Introduction

shape

Isomorphic and Async object validation library.

npm install --save @krab/shape'
import Shape from '@krab/shape'
const Shape = require('@krab/shape/common')

shape is used to vaidate data in an async manner. Doesn't matter if your checks are all synchronous, shape will check them in an async manner. This is very useful for validating data against external data sources. Example below:

import Shape from '@krab/shape';

// using async-await
async function createUser(data={}) {
  const validation = new Shape({
    username: async (username) => {
      if (isString(username) && username.length > 0) {
        const existing = await findUserByUsername(username);
        return existing ? 'username taken' : null;
      } else {
        return 'invalid';
      }
    },

    password: (password) => {
      return isString(password) && password.length >= 7 ? null : 'invalid';
    },

    repeat_password: (repeat_password, {password}) => {
      return repeat_password === password ? null : 'invalid';
    }
  });

  const errors = await validation.errors(data);
  // Errors will be 'null' if all checks pass.
  // If any check fails, errors will be an object that contains error messages.
  // Validation will also fail for keys in data which are not specified when
  // defining a validation

  if (errors) {
    throw errors;
  } else {
    return await insertNewPostInDB(data);
  }
}

// using promises
function createPost(data={}) {
  const validation = new Shape({
    author_id: async (authorId) => {
      const author = await findUserById(authorId);
      return author ? null : `invalid author_id: ${authorId}`;
    },

    title: (title) => {
      return isString(title) ? findPostByTitle(title).then((existing) => {
        return (
          existing ? 'title already taken' : (
            title.length === 0 ? 'title cannot be empty' : null
          )
        );
      }) : 'title must be string';
    },

    body: (body) => {
      if (!isArray(body) || body.filter((p) => isString(p)).length < body.length) {
        return 'body must be a list of paragraph strings';
      } else if (body.length === 0) {
        return 'body cannot be empty';
      } else {
        return null;
      }
    }
  });

  return validation.errors(data).then((errors) => {
    if (errors) {
      return Promise.reject(errors);
    } else {
      return insertNewPostInDB(data);
    }
  });
}

shape's People

Contributors

kapv89 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.