addFormat definitions ignored
See original GitHub issueHi, in my code (see below) I create the ajv instance in validation.ts
, and then import that instance into other files. However, I get an error message when calling validateSchema
from utils.ts
complaining about a missing format identifier email
(and indeed, ajv.formats
returns an empty object). When I add the format in the function validateSchema
itself it works - but why does it fail when I add the format in validation.ts
?
What version of Ajv are you using? Does the issue happen if you use the latest version?
"ajv": "^8.8.2"
Your code
// validation.ts
export const ajv = (new Ajv({
discriminator: true,
formats: {
email: <a valid regex expression>
}
}))
// utils.ts
import {JSONSchemaType} from "ajv";
import {ajv} from "./validation";
export function validateSchema<T>(
args: T,
schema: JSONSchemaType<T>,
) {
if(!schema.$id) throw Error("id missing")
const validate = ajv.getSchema(schema.$id) || ajv.compile(schema)
const isValid = validate(args)
if(isValid) return {isValid: true as const}
else {
return {
isValid: false as const,
errors: validate.errors
}
}
}
// auth.ts
import {JSONSchemaType} from "ajv";
export interface SignUpArgs {
firstName: string,
lastName: string,
email: string,
}
export const nameSchema: JSONSchemaType<string> = {
type: "string",
maxLength: 18,
minLength: 1,
}
// Error: unknown format "email" ignored in schema at path "#/properties/email"
export const emailSchema: JSONSchemaType<string> = {
type: "string",
format: "email",
maxLength: 60,
}
export const signUpArgsSchema: JSONSchemaType<SignUpArgs> = {
type: "object",
$id: "signUpArgsSchema",
properties: {
firstName: nameSchema,
lastName: nameSchema,
email: emailSchema
},
required: ["firstName", "lastName", "email"],
additionalProperties: false
}
Validation result, data AFTER validation, error messages
Error: unknown format "email" ignored in schema at path "#/properties/email"
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
ajv-formats - Ajv JSON schema validator
const ajv = new Ajv((formats: {date: true, time: true})) // to ignore "date" and "time" formats in schemas. Format validation mode (default is...
Read more >What are formats in LaTeX and how to manage them? - TeX
This adds two format definitions. In the TLPDB we are using key=value pairs, from which the fmtutil.cnf is generated. (Ignore the ...
Read more >Custom formatter for fastify - node.js - Stack Overflow
I want to add a custom schema formatter for fastify. import fastify from 'fastify' import AjvCompiler from '@fastify/ajv ...
Read more >FormatConditions.Add method (Excel) - Microsoft Learn
If Type is xlExpression, the Operator argument is ignored. Formula1, Optional, Variant, The value or expression associated with the ...
Read more >The Worksheet Class — XlsxWriter Documentation
FunctionType) – The user defined function to write the type data. ... As such, if you write an empty cell without formatting it...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For others who may have the same problem: What I did now is to put the schemas with formats into the definitions schema, and then added the definitions schema to ajv in the same file where I added the formats. Once you have added the definitions you can reference your schemas with formats with
$ref
.(I think there might be an issue with ajv when you add the formats in a file different from where you add the schema, although I do not understand how this is possible)
I am not sure what was the problem, but your example works in runkit: https://runkit.com/esp/61fd65af45df1c0008a15948
What was missing is type keyword alongside format, but I cannot reproduce the original error.
Going to close - please re-open or comment it it’s not resolved