0.1.1 • Published 7 years ago

redux-spec v0.1.1

Weekly downloads
3
License
ISC
Repository
github
Last release
7 years ago

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 store
  • definitions 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 state
  • action object the action
    • action.type string the action type
  • errors array the list of validation errors

Examples

const todoApp = combineReducers({
  todos,
  specErrors,
});

Returns object the new state

0.1.1

7 years ago

0.1.0

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago