Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vvgomes committed Sep 17, 2016
0 parents commit 03aea4f
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [ "es2015" ],
"env": {
"test": {
"plugins": [ "istanbul" ]
}
}
}
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

indent_style = space
indent_size = 2
42 changes: 42 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
parser: babel-eslint

extends:
- defaults
- defaults/configurations/eslint

env:
node: true
mocha: true
es6: true

rules:
semi:
- error
- always

object-curly-spacing:
- error
- always

space-before-function-paren:
- error
- always

complexity:
- error
- 2

eol-last:
- error

quotes:
- error
- double
- avoidEscape: true
allowTemplateLiterals: true

no-unused-vars:
- error
- argsIgnorePattern: "^_"

5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
dist/
coverage/
.nyc_output/
npm-debug.log
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lib/
test/
node_modules/
.*
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.0.0
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: node_js
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Predicado ✅

[![build status](https://travis-ci.org/vvgomes/predicado.svg?branch=master)](https://travis-ci.org/vvgomes/predicado)
[![npm version](https://img.shields.io/npm/v/predicado.svg)](https://www.npmjs.com/package/predicado)

Predicado is a small library for convenient predicate driven validations. It is build on top of the [data.validation](https://github.com/folktale/data.validation) module from [Folktale](http://folktalejs.org/).

## Getting Started

```
$ npm install predicado --save
```

```javascript
import { validate } from "predicado";

```

## License

Feel free to use this code as you please.
17 changes: 17 additions & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Validation from "data.validation";
import { curryN, reduce, length, always } from "ramda";

const Success = Validation.Success;
const Failure = Validation.Failure;

const validate = (thing, validations) => {
const initial =
Success(curryN(length(validations), always(thing)))

const run = (acc, v) =>
acc.ap(v.predicate(thing) ? Success(thing) : Failure([v.error]));

return reduce(run, initial, validations);
};

export default validate;
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "predicado",
"version": "0.0.1",
"description": "Predicate Driven Validations",
"main": "dist/predicado.js",
"author": "vvgomes",
"license": "ISC",
"repository": {
"type": "git",
"url": "https://github.com/vvgomes/predicado.git"
},
"dependencies": {
"ramda": "^0.19.1",
"data.validation": "^1.2.0"
},
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-eslint": "^6.1.2",
"babel-plugin-istanbul": "^2.0.1",
"babel-preset-es2015": "^6.6.0",
"eslint": "^3.5.0",
"eslint-config-defaults": "^9.0.0",
"mocha": "^2.4.5",
"nyc": "^8.1.0",
"sinon": "^1.17.5"
},
"scripts": {
"lint": "eslint {lib,test}/**/*",
"test": "nyc mocha --compilers js:babel-register --recursive",
"quality": "npm test && npm run lint",
"dist": "babel lib/ --out-dir dist/",
"prepublish": "npm test && npm run lint && npm run dist"
}
}
46 changes: 46 additions & 0 deletions test/validate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import assert from "assert";
import Validation from "data.validation";
import validate from "../lib/validate";
import { has, where, test } from "ramda";

const Success = Validation.Success;
const Failure = Validation.Failure;

describe("validate()", () => {

const validations = [
{ error: "Name must be provided.", predicate: has("name") },
{ error: "Password must be provided.", predicate: has("password") },
{ error: "Email must be in a valid format.", predicate: where({ email: test(/@/) })}
];

it("results in success when all predicates are true", () => {
const command = {
type: "addUser",
name: "John Doe",
email: "[email protected]",
password: "cupcake"
};

assert.deepEqual(
validate(command, validations),
Success(command)
);
});

it("results in failure when some predicates are false", () => {
const command = {
type: "addUser",
name: "John Doe",
email: "jdgmail.com"
};

assert.deepEqual(
validate(command, validations),
Failure([
"Password must be provided.",
"Email must be in a valid format."
])
);
});
});

0 comments on commit 03aea4f

Please sign in to comment.