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

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

Open
jashanbaatth opened this issue Jul 4, 2023 · 2 comments
Labels

Comments

@jashanbaatth
Copy link

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

@benjaminrae
Copy link
Contributor

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;

@jashanbaatth
Copy link
Author

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('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

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

2 participants