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

Example of async keyword with a standalone gen #389

Open
manuganji opened this issue Sep 24, 2023 · 0 comments
Open

Example of async keyword with a standalone gen #389

manuganji opened this issue Sep 24, 2023 · 0 comments

Comments

@manuganji
Copy link

manuganji commented Sep 24, 2023

Hi,

I tried to follow your doc on adding keywords with a compile function. When I try to generate the standalone validator, I get

ValueError: CodeGen: "code" for keyword0 not defined

This is the keyword code for reference:

ajv.addKeyword({
	keyword: "notExists",
	async: true,
	schema: true,
	valid: true,
	modifying: false,
	error: {
		message: ({data, schema, gen}) => {
			return gen.name(schema.column) + " already exists";
		}
	},
	type: ["string", "number"],
	schemaType: "object",
	compile: (schema) => {
		return async function (data) {
			try {
				const res = await fetch(`/api/ajv/exists`, {
					body: JSON.stringify({
						column: schema.column,
						table: schema.table,
						value: data,
					}),
					method: "GET",
				}).then<boolean>((res) => res.json());
				return !res;
			} catch (e) {
				return true;
			}
		};
	},
	metaSchema: {
		type: "object",
		properties: {
			column: { type: "string" },
			table: { type: "string" },
		},
		required: ["column", "table"],
	},
});```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant