Code Monkey home page Code Monkey logo

ramda-fantasy-validation's Introduction

ramda-fantasy-validation

Build Status npm version

Fantasy Land compatible Validation with Ramda.

Example

var Validation = require('ramda-fantasy-validation');
var R = require('ramda');

function validateName(name) {
  if (name.length > 0) {
    return Validation.of(name);
  }
  return Validation.failure(['Name is required.']);
}

function validateAge(age) {
  if (age >= 18) {
    return Validation.of(age);
  }
  return Validation.failure(['Age must be or over 18.']);
}

function createUser(name, age) {
  return { 'name': name, 'age': age };
}

// Validation.Success({"age": 99, "name": "mrkm4ntr"})
validateName('mrkm4ntr').map(R.curry(createUser)).ap(validateAge(99));
// or
Validation.liftAN(2, createUser)(validateName('mrkm4ntr'))(validateAge(99));

// Validation can accumulate error informations.
// Validation.Failure(["Name is required.", "Age must be or over 18."])
Validation.liftAN(2, createUser)(validateName(''))(validateAge(17));

ramda-fantasy-validation's People

Contributors

mrkm4ntr avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ramda-fantasy-validation's Issues

Easier syntax idea

Thanks for the great validation library. I was trying to find a cleaner way to use this and came up with this. If this is correct in all cases it might be good for the docs.

const R = require('ramda');
const Validation = require('ramda-fantasy-validation');

R.useWith(
 Validation.liftAN(3, functionToValidate),
 [
    validate('type', [String]),
    validate('ret', [Object, String]),
    validate('obj', [Object])
 ]
);

// where functionToValidate has those arguments
const functionToValidate = (type, ret, obj) => ...

// validate is whatever validation function you want, so long as it expects the parameter value to be supplied as the final argument. Example:
 const validate = module.exports.validate = R.curry((name, types, value) =>
   R.ifElse(
     v => R.any(type => R.is(type, v), types),
     Validation.of,
     _ => Validation.failure([`Requires ${name} as one of ${R.join(', ', R.map(t => R.type(t()), types))}, but got ${value}`]) // e.g. Validation.Failure('Requires ret as one of Object, String, but got 1')
   )(value)
);

fold function is incorrect

Should be
fold(f1, f2) { return this.isSuccess ? f1(this.value) : f2(this.value); }
but instead is
fold(f1, f2) { return this.success ? f1(this.value) : f2(this.value); }

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.