-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 03aea4f
Showing
11 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"presets": [ "es2015" ], | ||
"env": { | ||
"test": { | ||
"plugins": [ "istanbul" ] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "^_" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules/ | ||
dist/ | ||
coverage/ | ||
.nyc_output/ | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
lib/ | ||
test/ | ||
node_modules/ | ||
.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
language: node_js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
]) | ||
); | ||
}); | ||
}); |