-
I do not know if this is an issue . so I made it new Q & A in discussion. The issueI want to add unique validation rule to my validation schema with ajv, typebox and feathersjs v5. this is the code in validators file import { Ajv, addFormats } from "@feathersjs/schema";
import type { FormatsPluginOptions } from "@feathersjs/schema";
import { app } from "./app";
const formats: FormatsPluginOptions = [
"date-time",
"time",
"date",
"email",
"hostname",
"ipv4",
"ipv6",
"uri",
"uri-reference",
"uuid",
"uri-template",
"json-pointer",
"relative-json-pointer",
"regex",
];
let ajv = new Ajv({
allErrors: true,
messages: false,
});
ajv.addKeyword({
keyword: "unique",
type: "string",
async: true,
validate: async (schema: any, data: any) => {
try {
let row = await app
.get("dbclient")
.table(schema.table)
.where(schema.field, data)
.first();
return row !== undefined;
} catch (e) {
return true;
}
},
error: {
message: "out",
},
});
export const dataValidator: Ajv = addFormats(ajv, formats);
export const queryValidator: Ajv = addFormats(
new Ajv({
coerceTypes: true,
}),
formats,
); and when I need to use this with schema for example Type.Object({
name: Type.String({minLength:3, unique: {table: 'users', field: 'name'}})
}, {
$async: true
}) I see the next error
Info
Notes
The Question : any help with this ?
thank you. |
Beta Was this translation helpful? Give feedback.
Answered by
daffl
Oct 23, 2023
Replies: 1 comment 4 replies
-
When using properties with that keyword in the query validator it needs to be added there as well. const keywordUnique = {
keyword: 'unique',
type: 'string',
async: true,
validate: async (schema: any, data: any) => {
try {
let row = await app.get('dbclient').table(schema.table).where(schema.field, data).first()
return row !== undefined
} catch (e) {
return true
}
},
error: {
message: 'out'
}
} as const
export const dataValidator: Ajv = addFormats(new Ajv({}), formats)
export const queryValidator: Ajv = addFormats(
new Ajv({
coerceTypes: true
}),
formats
)
dataValidator.addKeyword(keywordUnique)
queryValidator.addKeyword(keywordUnique) |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
noor-tg
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When using properties with that keyword in the query validator it needs to be added there as well.