actus-state-validator
v0.2.0-alpha.4
Published
State validator and normalizer for actus (powered by joi)
Downloads
3
Readme
actus-state-validator
State validator and normalizer for actus (powered by joi)
Install
npm install actus-state-validator @hapi/joi
Examples
Usage
import { actus } from "actus";
import stateValidator from "actus-state-validator";
import Joi from "@hapi/joi";
actus([
stateValidator(Joi.object({...}), { /* joi validation options */ }),
{
state: {...},
actions: {...},
subscribers: [...]
}
]);
React Counter App
import { actus } from "actus";
import stateValidator from "actus-state-validator";
import logger from "actus-logger";
import Joi from "@hapi/joi";
import React from "react";
import ReactDOM from "react-dom";
actus([
logger(),
stateValidator(Joi.number().required().integer()),
{
state: 0,
actions: {
inc: ({ state }) => state + 1,
dec: ({ state }) => state - 1,
tryConvertingToString: ({ state }) => String(state),
trySettingToUndefined: () => undefined,
},
subscribers: [
({ state, actions }) => {
ReactDOM.render(
<>
<h1>{state}</h1>
<button onClick={() => actions.inc()}>+</button>
<button onClick={() => actions.dec()}>-</button>
<br />
<button onClick={() => actions.tryConvertingToString()}>
try converting to string (will be coerced to number)
</button>
<button onClick={() => actions.trySettingToUndefined()}>
try setting to undefined (will throw error)
</button>
</>,
document.querySelector("#root")
);
},
],
},
]);