import { types, createSchema, createSchemaObserver } from "redux-action-schema"
Types are used for schema definitions and action validation.
Any function that takes a value & returns a boolean can be used as a type, but we've provided a few common ones:
types.Object // matches any non-Array Object
types.Number // matches numbers
types.String // matches strings
types.Array // matches Arrays
types.Boolean // matches bools
types.Any // matches any non-null
types.OneOf([values...]) // matches members of array
types.ArrayOf(typeFn) // matches arrays where each element matches typeFn
types.OneOfType(typeA, typeB, ...) // matches any of its arguments
Each of the types also has an optional
variant that also matches null
and undefined
:
types.Object.optional // matches { foo: bar }, null, undefined
types.ArrayOf.optional(types.Number) // matches [1,2,3], null, undefined
types.ArrayOf(types.Number.optional) // matches [1, null, 3]
createSchema(actions, [options])
actions
(Array): A list of action definitions to validate against. See next section for more on this.[options]
(Object): Additional options for building the schema:namespace
(String |(String) => String
): rewrite action type strings handled by this schema.- The function form takes an action name and returns a modified action name:
schema = createSchema(["foo"], { namespace: (type) => type.toUpperCase() })
will useschema.actions.foo
but expect actions like{ type: "FOO" }
. - The string form prepends that string with an underscore to the original action name:
schema = createSchema(["foo"], { namespace: "ns"})
will useschema.actions.foo
but expect actions like{ type: "ns_foo" }
.
- The function form takes an action name and returns a modified action name:
({ actions, actionCreators, createReducer, createMiddleware }) = : a collection of objects and functions for working with and validating actions.
// no parameters
[typeName]
[typeName, docstring]
// single parameter
[typeName, typeFn]
[typeName, docstring, typeFn]
// named parameters
[typeName, docstring,
[paramName, typeFn],
[paramName, docstring, typeFn],
...
]
schema.actions.<ACTION_NAME>
An object mapping action names to their string representations. By default, these are identical, e.g. actions.foo === "foo"
; however, if the schema is created with a namespace parameter, that will be prepended to the action name, e.g. given { namespace: "ns" }
then actions.foo === "ns_foo"
.
An object mapping action names to functions that create actions of that type.
schema.actionCreators.foo(value) => { type: "foo", payload: value }
schema.createReducer(reducerMap, [initialState])
reducerMap
(Object) : map of action types to reducer functions, with signature(state, payload, action) => nextState
[initState]
(Any) : initial state provided to reducer functions.
((state = initState, action) => nextState
): a reducer function that handles all of the actions in the reducer map.
createReducer
throws if a key in the reducer map does not correspond to an action type, or if a value in the reducer map is not a function.
schema.createMiddleware([options])
[options]
ignorePayloads
(Boolean): Don't type check payloads, only check type names. You may want to use this for performance reasons. Default value:false
.onError
((action) => ()
): Callback called when an action doesn't validate. Default value:console.error("unknown action:", action)
ignoreActions
(Array): List of actions not in schema that shouldn't cause validation errors, e.g. those used by third-party middleware. Default value:["EFFECT_TRIGGERED", "EFFECT_RESOLVED", "@@router/UPDATE_LOCATION"]
, which are used by popular libraries redux-saga and react-redux-router.
(middleware
): a Redux middleware.
createSchemaObserver()
(observerMiddleware
): a redux middleware, with additional methods for schema generation.
observerMiddleware.schemaDefinitionString()
(String): source code for a schema definition.