-
Notifications
You must be signed in to change notification settings - Fork 11
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
Showing
74 changed files
with
1,276 additions
and
1,435 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 |
---|---|---|
|
@@ -7,7 +7,6 @@ dev | |
version.json | ||
version.txt | ||
.version | ||
docs | ||
documentation/MAIN.md | ||
documentation/assets/**/*.css | ||
.DS_Store |
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 |
---|---|---|
|
@@ -8,7 +8,6 @@ dev | |
.travis.yml | ||
webpack.config.js | ||
yarn.lock | ||
docs | ||
_docpress | ||
_docs | ||
flow-typed | ||
|
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Empty file.
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,78 @@ | ||
# ![Passable](https://cdn.rawgit.com/fiverr/passable/master/documentation/assets/img/logo.png?raw=true "Passable") | ||
|
||
Declarative data validations. | ||
|
||
[![npm version](https://badge.fury.io/js/passable.svg)](https://badge.fury.io/js/passable) [![Build Status](https://travis-ci.org/fiverr/passable.svg?branch=master)](https://travis-ci.org/fiverr/passable) | ||
|
||
|
||
- [Documentation homepage](https://fiverr.github.io/passable/) | ||
- [Try it live](https://stackblitz.com/edit/passable-example?file=validate.js) | ||
- [Getting started](getting_started/writing_tests). | ||
|
||
## What is Passable? | ||
Passable is a library for JS applications for writing validations in a way that's structured and declarative. | ||
|
||
Inspired by the syntax of modern unit testing framework, passable validations are written as a spec or a contract, that reflects your form structure. | ||
Your validations run in production code, and you can use them in any framework (or without any framework at all). | ||
|
||
The idea behind passable is that you can easily adopt its very familiar syntax, and transfer your knowledge from the world of testing to your form validations. | ||
|
||
Much like most testing frameworks, Passable comes with its own assertion function, [enforce](./enforce/readme.md), all error based assertion libraries are supported. | ||
|
||
## Key features | ||
1. [Non failing tests](test/warn_only_tests). | ||
2. [Conditionally running tests](test/specific). | ||
3. [Async validations](test/async). | ||
4. [Test callbacks](getting_started/callbacks). | ||
|
||
--- | ||
|
||
## Syntactic differences from testing frameworks | ||
|
||
Since Passable is running in production environment, and accommodates different needs, some changes to the basic unit test syntax have been made, to cover the main ones quickly: | ||
|
||
- Your test function is not available globally, it is an argument passed to your suite's callback. | ||
- Each test has two string values before its callback, one for the field name, and one for the error returned to the user. | ||
- Your suite accepts another argument after the callback - name (or array of names) of a field. This is so you can run tests selectively only for changed fields. | ||
|
||
```js | ||
// validation.js | ||
import passable, { enforce } from 'passable'; | ||
|
||
const validation = (data) => passable('NewUserForm', (test) => { | ||
|
||
test('username', 'Must be at least 3 chars', () => { | ||
enforce(data.username).longerThanOrEquals(3); | ||
}); | ||
|
||
test('email', 'Is not a valid email address', () => { | ||
enforce(data.email) | ||
.isNotEmpty() | ||
.matches(/[^@]+@[^\.]+\..+/g); | ||
}); | ||
}); | ||
|
||
export default validation; | ||
``` | ||
|
||
```js | ||
// myFeature.js | ||
import validation from './validation.js'; | ||
|
||
const res = validation({ | ||
username: 'example', | ||
email: '[email protected]' | ||
}); | ||
|
||
res.hasErrors() // returns whether the form has errors | ||
res.hasErrors('username') // returns whether the 'username' field has errors | ||
res.getErrors() // returns an object with an array of errors per field | ||
res.getErrors('username') // returns an array of errors for the `username` field | ||
``` | ||
|
||
## "BUT HEY! I ALREADY USE X VALIDATION LIBRARY! CAN IT WORK WITH PASSABLE?" | ||
As a general rule, Passable works similarly to unit tests in term that if your test throws an exception, it is considered to be failing. Otherwise, it is considered to be passing. | ||
|
||
There are a [few more ways](https://fiverr.github.io/passable/test/how_to_fail) to handle failures in order to ease migration, and in most cases, you can move your validation logic directly to into Passable with only a few adjustments. | ||
|
||
For example, if you use a [different assertion libraries](https://fiverr.github.io/passable/compatability/assertions) such as `chai` (expect) or `v8n`, you can simply use it instead of enforce, and it should work straight out of the box. |
Binary file not shown.
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,23 @@ | ||
Object.assign(window, passable); | ||
|
||
setTimeout(() => { | ||
console.log(`All passable functions are exposed globally, | ||
you can try passable in your console.`); | ||
|
||
console.log(`PASSABLE VERSION:`, passable.VERSION); | ||
|
||
console.table(Object.keys(passable).map((item) => [item, typeof passable[item]])); | ||
|
||
console.log(`You can try: | ||
const data = { | ||
useanme: 'example' | ||
}; | ||
passable('FormName', () => { | ||
test('username', 'Must be at least 4 chars', () => { | ||
enforce(data.username).longerThanOrEquals(4); | ||
}); | ||
}); | ||
`) | ||
}, 1000); |
File renamed without changes
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,28 @@ | ||
.markdown-section code { | ||
font-size: 85%; | ||
color: #26B4AD; | ||
} | ||
|
||
.app-name { | ||
padding: 10px; | ||
} | ||
|
||
.sidebar-nav { | ||
padding-left: 10px; | ||
} | ||
|
||
body, .anchor span, a.anchor { | ||
color: #3C4464; | ||
} | ||
|
||
.token.function,.token.keyword { | ||
color: #26B4AD | ||
} | ||
|
||
.token.boolean, .token.number { | ||
color: #59d4e8; | ||
} | ||
|
||
.markdown-section a:hover { | ||
color: #3a6b96; | ||
} |
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,16 @@ | ||
|
||
* Getting Started | ||
* [Installation](./getting_started/installation.md) | ||
* [Writing tests](./getting_started/writing_tests.md) | ||
* [Passable callbacks](./getting_started/callbacks.md) | ||
* [The `result` object](./getting_started/result.md) | ||
* [The `test` Function](./test/index.md) | ||
* [How to fail a test](./test/how_to_fail.md) | ||
* [Warn only tests](./test/warn_only_tests.md) | ||
* [Skipping tests](./test/specific.md) | ||
* [Async tests](./test/async.md) | ||
* [Test Utilities](./utilities/README.md) | ||
* [Assertions with `enforce`](./enforce/README.md) | ||
* [Enforce Rules](./enforce/rules/README.md) | ||
* [Custom Enforce Rules](./enforce/rules/custom.md) | ||
* [Using with assertions libraries](./compatability/assertions.md) |
File renamed without changes.
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
Oops, something went wrong.