all tests pass
This commit is contained in:
parent
7e8c2ef171
commit
d443441048
@ -2,6 +2,7 @@
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@yanick/updeep-remeda": "^2.2.0",
|
||||
"ajv": "^8.12.0",
|
||||
"expect-type": "^0.16.0",
|
||||
"immer": "^9.0.15",
|
||||
"json-schema-shorthand": "^2.0.0",
|
||||
|
25
src/Updux.ts
25
src/Updux.ts
@ -22,6 +22,7 @@ import {
|
||||
EffectMiddleware,
|
||||
} from './effects.js';
|
||||
import buildSchema from './schema.js';
|
||||
import Ajv from 'ajv';
|
||||
|
||||
export type Mutation<A = AnyAction, S = any> = (
|
||||
payload: A extends {
|
||||
@ -32,6 +33,19 @@ export type Mutation<A = AnyAction, S = any> = (
|
||||
action: A,
|
||||
) => (state: S) => S | void;
|
||||
|
||||
|
||||
function buildValidateMiddleware(schema) {
|
||||
// @ts-ignore
|
||||
const ajv = new Ajv();
|
||||
const validate = ajv.compile(schema);
|
||||
return (api) => next => action => {
|
||||
next(action);
|
||||
const valid = validate(api.getState());
|
||||
|
||||
if (!valid) throw new Error("validation failed after action " + JSON.stringify(action) + "\n" + JSON.stringify(validate.errors));
|
||||
}
|
||||
}
|
||||
|
||||
export default class Updux<D extends DuxConfig> {
|
||||
#mutations = [];
|
||||
|
||||
@ -174,6 +188,7 @@ export default class Updux<D extends DuxConfig> {
|
||||
createStore(
|
||||
options: Partial<{
|
||||
preloadedState: DuxState<D>;
|
||||
validate: boolean;
|
||||
}> = {},
|
||||
): EnhancedStore<DuxState<D>> & AugmentedMiddlewareAPI<D> {
|
||||
const preloadedState = options.preloadedState;
|
||||
@ -184,10 +199,18 @@ export default class Updux<D extends DuxConfig> {
|
||||
this.selectors,
|
||||
);
|
||||
|
||||
const middleware = [effects];
|
||||
|
||||
if (options.validate) {
|
||||
middleware.unshift(
|
||||
buildValidateMiddleware(this.schema)
|
||||
)
|
||||
}
|
||||
|
||||
const store = configureStore({
|
||||
reducer: this.reducer,
|
||||
preloadedState,
|
||||
middleware: [effects],
|
||||
middleware,
|
||||
});
|
||||
|
||||
const dispatch: any = store.dispatch;
|
||||
|
@ -29,3 +29,17 @@ test('schema default inherits from initial state', () => {
|
||||
|
||||
expect(dux.schema.default).toEqual(8);
|
||||
});
|
||||
|
||||
test('validate', () => {
|
||||
const dux = new Updux({
|
||||
initialState: 12,
|
||||
actions: {
|
||||
doItWrong: null,
|
||||
},
|
||||
});
|
||||
|
||||
dux.addMutation('doItWrong', () => () => 'potato' as any);
|
||||
|
||||
const store = dux.createStore({ validate: true });
|
||||
expect(() => store.dispatch.doItWrong()).toThrow();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user