import { use, r } from '@marblejs/core';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { userSchema } from './user.schema.ts';
const effect$ = r.pipe(
r.matchPath('/'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
requestValidator$({ body: userSchema }),
// ..
)));
For more validation use cases and recipes, visit Validation chapter.
You can also reuse the same schema for Events validation if you want.
import { matchEvent, act } from '@marblejs/core';
import { WsEffect } from '@marblejs/websockets';
import { eventValidator$, t } from '@marblejs/middleware-io';
import { userSchema } from './user.schema.ts';
const postUser$: WsEffect = event$ =>
event$.pipe(
matchEvent('CREATE_USER'),
act(eventValidator$(userSchema)),
act(event => { ... }),
);
The inferred req.body / event.payload type of provided schema, will be of the following form:
type User = {
id: string;
firstName: string;
lastName: string;
roles: ('ADMIN' | 'GUEST')[];
};
Please note that eacheventValidator$must be applied inside act operator to prevent closing of the main observable stream.
Validation errors
Lets take a look at the default reported validation error thrown by eventValidator$ . Let's assume that client passed wrong values for firstName and roles fields.