Skip to content

Commit

Permalink
Merge pull request #15 from lexich/validation
Browse files Browse the repository at this point in the history
Validation option to enpoint config
  • Loading branch information
lexich committed Sep 29, 2015
2 parents 83d769f + d015612 commit 28ba97c
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 8 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function (state, action) {
> *type*: Array<Function>
> *default*: null
> *example*
}
```js
{
user: "/user/info",
Expand All @@ -148,10 +148,10 @@ function (state, action) {
name ? cb() : dispatch(actions.user(cb));
},
function({actions, dispatch, getState}, cb) {
const {user: {data: {name}}, profile: {data: {uuid}} = getState();
const {user: {data: {name}}, profile: {data: {uuid}}} = getState();
uuid ? cb() : dispatch(actions.profile({name}, cb));
}
]
],
options: function(url, params, getState) {
const {user: {data: {uuid}}} = getState();
return { ...params, body: { ...params.body, uuid }};
Expand All @@ -160,6 +160,26 @@ function (state, action) {
}
```

- @param **options.{endpoint}.validate(data, cb) - validation function
> *type*: Function(data, cb)
> *default*: null
> *example*
```js
{
test: {
url: "/api/test",
validation: (data, cb) {
// check data format
let error;
if (data instanceOf Array) {
error = "Data must be array";
}
cb(error);
}
}
}
```

#### reduxApi object
`reduxApi` initializer returns non initialized object. You need to call `init` for initilize it.
```js
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-api",
"version": "0.6.1",
"version": "0.6.2",
"main": "dist/redux-api.min.js",
"dependencies": {}
}
6 changes: 6 additions & 0 deletions dist/redux-api.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/redux-api.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/redux-api.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/redux-api.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-api",
"version": "0.6.1",
"version": "0.6.2",
"author": {
"name": "Efremov Alex",
"email": "lexich121@gmail.com",
Expand Down
3 changes: 3 additions & 0 deletions src/actionFn.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {

fetchResolver(0, fetchResolverOpts,
(err)=> err ? pubsub.reject(err) : meta.holder.fetch(urlT, opts)
.then((data)=> !meta.validation ? data :
new Promise((resolve, reject)=> meta.validation(data,
(err)=> err ? reject(err) : resolve(data))))
.then((data)=> {
dispatch({ type: actionSuccess, syncing: false, data });
each(meta.broadcast, (btype)=> dispatch({type: btype, data}));
Expand Down
63 changes: 63 additions & 0 deletions test/actionFn_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,69 @@ describe("actionFn", function() {
expect(expectedEvent).to.have.length(0);
});
});
it("check success validation", function() {
let expData, counter = 0;
const meta = {
holder: {fetch: fetchSuccess},
validation(data, cb) {
counter++;
expData = data;
cb();
}
};
const expectedEvent = [{
type: ACTIONS.actionFetch,
syncing: false
}, {
type: ACTIONS.actionSuccess,
data: {msg: "hello"},
syncing: false
}];

const api = actionFn("/test/:id", "test", null, ACTIONS, meta);
return new Promise((resolve)=> {
api(resolve)(function(msg) {
expect(expectedEvent).to.have.length.above(0);
const exp = expectedEvent.shift();
expect(msg).to.eql(exp);
}, getState);
}).then(()=> {
expect(expectedEvent).to.have.length(0);
expect(counter).to.eql(1);
expect(expData).to.eql({ msg: "hello" });
});
});
it("check unsuccess validation", function() {
let expData, counter = 0;
const meta = {
holder: {fetch: fetchSuccess},
validation(data, cb) {
counter++;
expData = data;
cb("invalid");
}
};
const expectedEvent = [{
type: ACTIONS.actionFetch,
syncing: false
}, {
type: ACTIONS.actionFail,
error: "invalid",
syncing: false
}];
const api = actionFn("/test/:id", "test", null, ACTIONS, meta);
return new Promise((resolve)=> {
api(resolve)(function(msg) {
expect(expectedEvent).to.have.length.above(0);
const exp = expectedEvent.shift();
expect(msg).to.eql(exp);
}, getState);
}).then(()=> {
expect(expectedEvent).to.have.length(0);
expect(counter).to.eql(1);
expect(expData).to.eql({ msg: "hello" });
});
});
it("check prefetch option", function() {
const checkPrefetch = [];
const meta = {
Expand Down

0 comments on commit 28ba97c

Please sign in to comment.