Adding custom keyword through ajv "unique" , need to access repository.
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
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;
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