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

Improvement: Display detailed messages when validation fails #2526

Open
AM1988 opened this issue Jan 9, 2025 Discussed in #2497 · 0 comments
Open

Improvement: Display detailed messages when validation fails #2526

AM1988 opened this issue Jan 9, 2025 Discussed in #2497 · 0 comments

Comments

@AM1988
Copy link

AM1988 commented Jan 9, 2025

Discussed in #2497

Originally posted by AM1988 October 4, 2024
Hi there.

I have a class for data validation
`export class SchemaValidator {
static ajv = new Ajv({ allErrors: true, verbose: true });

public static validate<T>(schema: SchemaFile, data: T): boolean {
    const schemaContent = JSON.parse(fs.readFileSync(path.join(__dirname, 'path', schema), 'utf-8'));
    if (!this.ajv.validateSchema(schemaContent)) {
        console.error(`Schema is not valid.`);
    }
    const isValid = this.ajv.validate(schemaContent, data);

    if (!isValid) {
        const errorMessages = this.ajv.errorsText();
        logger.error(`Validation failed : ${errorMessages}`);
        return false;
    }
    return true;
}

}`
I use JSON schema described in a separate file.
How to get the full output when validation fails so that I can understand which part exactly does not match?

For example, values in the enum did not match, or input data had some additional properties, missing properties, etc.

Right now I see only data.payload.something should be equal to one of the allowed values

It does not have info about original data that does not pass schema validation.

It returns messages like
data.sources[0].version should match pattern "^.{555}$", data.sources[1].version should match pattern "^.{555}$", data.sources[2].version should match pattern "^.{555}$" but you have no clue what exactly data.sources[0].version is.

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