npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bs-validation

v2.0.0

Published

BucklesScript bindings to the validationjs library

Downloads

7

Readme

Build Status Coverage Status

bs-validation

A BuckleScript implementation of the Folktale validation applicative

NOTE: These are not bindings, this a ReasonML implementation of the Validation applicative.

Why?

I wanted a way to do validations for my server side project.

Status

Not all of the Folktale/Validation functions will be implemented. Here is a list of the currently implemented functions:

  • [x] map
  • [x] apply
  • [x] unsafeGet
  • [x] getOrElse
  • [x] orElse
  • [x] concat
  • [x] fold
  • [x] swap
  • [x] bimap
  • [x] mapFailure
  • [x] toOption
  • [ ] ~~merge~~ see note in the code.

All implemented functions are found in src/Validation.re, They are all documented with their Folktale style doc strings.

How do I install?

  1. Add the bs-validation package to your project.
yarn add bs-validation
  1. Add bs-validation to your bsconfig.json
{
  "dependencies": [ "bs-validation" ]
}
  1. Enjoy!

Usage

The library is exposed as a functor which accepts modules that implement the following type interface:

module type Foldable = {
  type t('a);
  let concat: (t('a), t('a)) => t('a);
};

All of the examples use an array based implementation of the Foldable type:

module FoldableArray = {
  type t('a) = array('a);
  let concat = (x, y) => Belt_Array.concat(x, y);
};

You import the module into your project by calling the Validation functor with your version of the Foldable type.

module V = Validation.Make_validation(FoldableArray);

Then you can use it to validate all of your things!

let lengthError = "Password must have more than 6 characters.";
let strengthError = "Password must contain a special character.";

let isPasswordLongEnough = (password) =>
  String.length(password) > 6
    ? V.Success(password)
    : V.Failure([|lengthError|]);

let isPasswordStrongEnough = (password) => {
  let regex = [%bs.re "/[\\W]/"];
  Js.Re.test(password, regex)
    ? V.Success(password)
    : V.Failure([|strengthError|])
};

let isPasswordValid = (password) => {
  V.Success()
  |> V.concat(isPasswordLongEnough(password))
  |> V.concat(isPasswordStrongEnough(password))
  |> V.map((_) =>password)
};


describe("Folketale password validation example", () => {
  test("should return the password", () => {
    let password = "rosesarered$andstuff";
    switch (isPasswordValid(password)) {
    | Failure(f) => { Js.log(f); fail("unexpected_failure") }
    | Success(p) => Expect.expect(p) |> Expect.toBe(password)
    }
  });

  test("should return a single item failure", () => {
    let password = "rosesarered";
    switch (isPasswordValid(password)) {
    | Failure(f) => Expect.expect(f) |> Expect.toBeSupersetOf([|strengthError|])
    | Success(_) => fail("unexpected_success")
    }
  });

  test("should return 2 items in the failure list", () => {
    let password = "foo";
    switch (isPasswordValid(password)) {
    | Failure(f) => {
        Expect.expect(f)
        |> Expect.toBeSupersetOf([|lengthError, strengthError|])
      }
    | Success(_) => fail("unexpected_success")
    }
  });
});