Skip to content
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

Open
ghost opened this issue Oct 6, 2020 · 3 comments
Open

How to check validate on method find and get ? #28

ghost opened this issue Oct 6, 2020 · 3 comments
Labels

Comments

@ghost
Copy link

ghost commented Oct 6, 2020

I using @feathers-plus/validate-joi but i can not check validate on get and find.
how to validate on find and get ?

@mkovel
Copy link

mkovel commented Oct 7, 2020

Hi, @garfewsab.
You can try my approach

// 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(),
};

@mkovel
Copy link

mkovel commented Oct 7, 2020

Sorry for the mess in code ;)

@stale
Copy link

stale bot commented Jun 11, 2021

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.

@stale stale bot added the wontfix label Jun 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant