Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Minor] export enforce as a standalone module #58

Merged
merged 2 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/Passable.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/Passable.min.js.map

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions documentation/enforce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ The `enforce` function runs your data against different rules and conditions. It

When using enforce, you do not have to return the result (although you may), each of your enforce tests updates the result of the whole pass. You may also use multiple enforces in the same pass function, they will run in sequence.

## Consuming `enforce`
You can consume the `enforce` function in two ways:

* Manually importing `enforce` along with `passable`

```js
import passable, {enforce} from 'passable';

Passable('enforcement', (pass) => {
pass('test', 'enforce example', () => {
enforce(4).isNumber();
});
});
```
Direct import is the newer standard, and is intended to support future functionality of leaner validation syntax.

* `enforce` is the second argument of the passable function
```js
Passable('enforcement', (pass, enforce) => {
pass('test', 'enforce example', () => {
enforce(4).isNumber();
});
});
```

```js
Passable('enforcement', (pass, enforce) => {
pass('test', 'multiple enforces', () => {
Expand Down
2 changes: 1 addition & 1 deletion documentation/getting_started/pass/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The pass function is a single test in your validations. It is similar to unit te
You can have multipe `pass` functions for each field, each with a different error.

```js
Passable('MyForm', (pass, enforce) => {
Passable('MyForm', (pass) => {
pass('name', 'should be ...', () => {...});
pass('name', 'should be ...', () => {...});
pass('age', 'should be ...', () => {...});
Expand Down
2 changes: 1 addition & 1 deletion documentation/getting_started/pass/specific.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In the following example, only First pass is going to run. Second will be skippe
const result = SpecificTests(['First']);

function SpecificTests (specific) {
return Passable('SpecificTests', specific, (pass, enforce) => {
return Passable('SpecificTests', specific, (pass) => {
pass('First', 'should pass', () => {...});
pass('Second', 'should be skipped', () => {...});
});
Expand Down
4 changes: 2 additions & 2 deletions documentation/getting_started/pass/warn_only_passes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ This will leave `hasValidationErrors` unchanged (other tests may have set it to

If no flag is added, your pass function will default to `fail`.

```
Passable('WarnAndPass', (pass, enforce) => {
```js
Passable('WarnAndPass', (pass) => {
pass('WarnMe', 'Should warn and not fail', 'warn', () => false);
});
```
Expand Down
4 changes: 2 additions & 2 deletions documentation/getting_started/writing_tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Much like when writing unit-tests, writing validations with Passable is all about knowing in advance which values you expect to get. The structure is very similar to the familiar unit test `describe/it/expect` combo, only that with Passable the functions you will mostly run are `Passable/pass/enforce`.

* `Passable` - the wrapper for your form validation, much like the describe function in unit tests.
* `pass` - a single tests, most commonly a single field, much like the it function in unit tests.
* `enforce` - the function which gets and enforces the data model compliance, similar to the expect function in unit tests.
* `pass` - a single tests, most commonly a single field, much like the it function in unit tests. [More about pass](./pass/index.md)
* `enforce` - the function which gets and enforces the data model compliance, similar to the expect function in unit tests. [More about enforce](../enforce/README.md);

The most basic test would look somewhat like this:

Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ function passable(name: string, ...args: PassableArguments) {
}
passable.VERSION = version;

export default passable;
export default passable;
export { default as enforce } from './enforce';
31 changes: 31 additions & 0 deletions src/spec/passable.exports.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
import { expect } from 'chai';
import passable from '../index';
const passableExports = require('../index');
import { version } from '../../package.json';

describe('Test Passable\'s exports', () => {
describe('Test passable\'s default export', () => {
it('Default export should output correct version number', () => {
expect(passable.VERSION).to.equal(version);
});

it('Default export should output correct version number', () => {
expect(passableExports.default.VERSION).to.equal(version);
});

it('Default export name should be `passable`', () => {
expect(passableExports.default.name).to.equal('passable');
});
});

describe('Test enforce export', () => {
it('enforce should be exported as a function', () => {
expect(passableExports.enforce).to.be.a('function');
});

it('enforce function name should be enforce', () => {
expect(passableExports.enforce.name).to.equal('enforce');
});
});
});
11 changes: 0 additions & 11 deletions src/spec/passable.misc.spec.js

This file was deleted.

8 changes: 4 additions & 4 deletions src/spec/usecase/passable.usecase.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import dist from '../../../dist/Passable.min';
import dev from '../../index';
import dist, * as distExports from '../../../dist/Passable.min';
import dev, * as devExports from '../../index';
import usecase_a from './usecase_a';
import usecase_b from './usecase_b';
import usecase_c from './usecase_c';
Expand Down Expand Up @@ -33,8 +33,8 @@ describe('Test Passable full usecase', () => {
});

it('should return correct response object for case: d', () => {
const devData = usecase_d(dev),
distData = usecase_d(dist);
const devData = usecase_d(dev, devExports.enforce),
distData = usecase_d(dist, distExports.enforce);

expect(devData.response).to.deep.equal(devData.expect);
expect(distData.response).to.deep.equal(devData.expect);
Expand Down
16 changes: 13 additions & 3 deletions src/spec/usecase/usecase_d.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export default (passable) => {
export default (passable, enforce) => {

/*
Test cases:
* Inline custom rules
* Import enforce directly from passable
*/

const response = passable('case_d', (pass, enforce) => {
const response = passable('case_d', (pass) => {
pass('field_1', 'hello should equal hello', () => {
enforce('hello').allOf({
stringEquals: (value) => typeof value === 'string' && 'hello' === value
Expand All @@ -17,6 +18,10 @@ export default (passable) => {
stringEquals: (value) => typeof value === 'string' && 'hell no' === value
});
});

pass('field_3', 'should be longer than 2', () => {
enforce(4).largerThan(2);
});
});

const expect = {
Expand All @@ -25,7 +30,7 @@ export default (passable) => {
hasValidationWarnings: false,
failCount: 1,
warnCount: 0,
testCount: 2,
testCount: 3,
testsPerformed: {
field_1: {
testCount: 1,
Expand All @@ -36,6 +41,11 @@ export default (passable) => {
testCount: 1,
failCount: 1,
warnCount: 0
},
field_3: {
testCount: 1,
failCount: 0,
warnCount: 0
}
},
validationErrors: {
Expand Down