loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Adding custom keyword through ajv "unique" , need to access repository.

Open jashanbaatth opened this issue 2 years ago • 2 comments

Describe the bug

I am adding custom keyword "unique" in loopback 4. It will check whether the entered value is present in db or not.

How can I access repository or request context in "unique" keyword definition or validation.

Logs

No response

Additional information

No response

Reproduction

https://ajv.js.org/guide/async-validation.html

jashanbaatth avatar Jul 04 '23 12:07 jashanbaatth

Have you tried defining properties as unique on the model?

I believe you can do something like this, at least with Postgres, I don't know if it works in all connectors

    @property({
      type: 'string',
      index: {
        unique: true,
      },
    })
    property: string;

benjaminrae avatar Jul 05 '23 08:07 benjaminrae

I want to create a custom keyword, in which I can access repository to run some query. I am using mongodb.

@property({ type: 'string', jsonSchema: { customKeyword: true, }, }) property: string;

keyword definition :-

ctx .bind<AjvKeyword>('ajv.keywords.smallNumber') .to({ name: 'smallNumber', // name of the keyword type: 'number', validate: (schema: unknown, data: number) => { // The number is smaller than 10 const data = async this.repo.find({where:{conditions}}); return !!data; }, }) .tag(RestTags.AJV_KEYWORD);

I want to do something like this

jashanbaatth avatar Jul 06 '23 05:07 jashanbaatth