-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to check validate on method find and get ? #28
Comments
Hi, @garfewsab. // users.hooks.ts
import validator from "./users.validator";
export default {
before: {
all: [],
find: [
validator.find()
],
get: [
validator.get()
],
create: [
validator.create()
],
}
}
// users.validator.ts
import { object as JoiObject, string as JoiString } from '@hapi/joi';
import validate from '@feathers-plus/validate-joi';
import {
name,
JoiPaginationSchema,
JoiOptions,
} from '../validatorSchemas';
// only 'name' query string var allows
const findSchema = JoiObject().keys({
query: {
name: JoiString(),
},
});
// all query string vars forbidden
const getSchema = JoiObject().keys({});
const createSchema = JoiObject().keys({
data: {
name: name.required(),
author: JoiObjectId.required(),
},
});
export default {
find: () => validate.form(findSchema, {...JoiOptions, convert: true}),
get: () => validate.form(getSchema, JoiOptions),
create: () => validate.form(createSchema, JoiOptions),
};
// validatorSchemas.ts
import {
number as JoiNumber,
} from '@hapi/joi';
import { HookContext, Query } from '@feathersjs/feathers';
import { ValidateJoiOptions } from '@feathers-plus/validate-joi';
interface JoiMixedContext {
query?: Query;
data?: HookContext['data'];
}
export const JoiOptions: ValidateJoiOptions = {
abortEarly: false,
convert: false,
getContext(context: HookContext): JoiMixedContext {
const newCtx: JoiMixedContext = {};
if (Object.keys(context.params?.query || {}).length) {
newCtx.query = context.params.query;
}
if (context.data) {
newCtx.data = context.data;
}
return newCtx;
},
setContext: (context: HookContext, convertedValues: any): void => {
if (convertedValues.query) {
context.params.query = convertedValues.query;
}
if (convertedValues.data) {
context.data = convertedValues.data;
}
},
};
export const JoiPaginationSchema = {
$limit: JoiNumber().min(1),
$skip: JoiNumber(),
};
|
Sorry for the mess in code ;) |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Apologies if the issue could not be resolved. FeathersJS ecosystem modules are community maintained so there may be a chance that there isn't anybody available to address the issue at the moment. For other ways to get help see here. |
I using @feathers-plus/validate-joi but i can not check validate on get and find.
how to validate on find and get ?
The text was updated successfully, but these errors were encountered: