redux-spec
v0.1.1
Published
Specify redux store structure and generate sample data for testing.
Downloads
11
Readme
redux-spec
redux-spec is a library to validate a redux store against a json schema, and to generate sample data for testing purposes.
Example
// specs.js
const todoSpec = {
id: 'todo',
type: 'object',
properties: {
id: {
$ref: 'positiveInt'
},
text: {
type: 'string',
minLength: 1,
},
completed: {
type: 'boolean',
}
},
required: ['id', 'text', 'completed']
};
const todosSpec = {
id: 'todos',
type: 'array',
items: { $ref: 'todo'}
};
const positiveInt = {
id: 'positiveInt',
type: 'integer',
minimum: 0,
exclusiveMinimum: false,
};
// index.js
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { specErrors, combineSpecs, createValidator } from 'redux-spec';
const todoApp = combineReducers({
todos,
visibilityFilter,
specErrors,
});
const storeSpec = combineSpecs(
{ todosSpec },
{ todoSpec, positiveInt },
);
const validator = createValidator(storeSpec);
const store = createStore(todoApp,
applyMiddleware(validator)
);
API
combineSpecs
Combine specs into a single spec.
Parameters
specs
object the top level specs that are required by the redux storedefinitions
object the reference specs used by top level specs
Examples
const storeSpec = combineSpecs(
{ todosSpec },
{ todoSpec, positiveInteger },
);
Returns object a full json-schema specification
createValidator
Create a json-schema validator that used as middleware in a redux store
Parameters
spec
object the json-schema spec to validate
Returns function a middleware function to be passed to applyMiddleware
generate
Generates sample data from json-schema spec
Parameters
spec
object the json-schema spec to validate
specErrors
The reducer used to store validation errors in the redux store;
Parameters
state
array?= [] the current stateaction
object the actionaction.type
string the action type
errors
array the list of validation errors
Examples
const todoApp = combineReducers({
todos,
specErrors,
});
Returns object the new state