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

schemaErrorFormatter uses an insufficient validation type #4093

Closed
2 tasks done
zedeus opened this issue Jun 29, 2022 · 1 comment · Fixed by #4094
Closed
2 tasks done

schemaErrorFormatter uses an insufficient validation type #4093

zedeus opened this issue Jun 29, 2022 · 1 comment · Fixed by #4094
Labels
feature request New feature to be added help wanted Help the community by contributing to this issue semver-minor Issue or PR that should land as semver minor typescript TypeScript related

Comments

@zedeus
Copy link

zedeus commented Jun 29, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.2.0

Plugin version

No response

Node.js version

16.15.1

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Irrelevant

Description

The FastifySchemaValidationError type used for the schemaErrorFormatter callback has a small subset of the fields available from the full Ajv validation error object.

export interface FastifySchemaValidationError {
message?: string;
instancePath: string;
}

This is the proper object:

fastify/fastify.d.ts

Lines 178 to 190 in 8fccc46

declare module '@fastify/error' {
interface FastifyError {
validation?: ValidationResult[];
}
}
export interface ValidationResult {
keyword: string;
instancePath: string;
schemaPath: string;
params: Record<string, string | string[]>;
message?: string;
}

Steps to Reproduce

import Fastify from "fastify";

const fastify = Fastify({
	schemaErrorFormatter: (errors, dataVar) => {
		const [error] = errors;

		const path = (error.instancePath ?? "")
			.replace("/", ".")
			.replace(/\/(\d)/, "[$1]");

		let { message } = error;
		if (error.keyword === "enum") {
			const values = error.params.allowedValues as string[];
			message += `: ${values.join(", ")}`;
		}

		return new Error(`${dataVar}${path} ${message.replace(/"/g, "'")}`);
	}
});
test.ts:12:13 - error TS2339: Property 'keyword' does not exist on type 'FastifySchemaValidationError'.

12   if (error.keyword === "enum") {
               ~~~~~~~

test.ts:13:25 - error TS2339: Property 'params' does not exist on type 'FastifySchemaValidationError'.

13    const values = error.params.allowedValues as string[];

Workaround:

import Fastify, { ValidationResult } from "fastify";

const fastify = Fastify({
	schemaErrorFormatter: (errors, dataVar) => {
		const [error] = errors as unknown as ValidationResult[];

		const path = (error.instancePath ?? "")
			.replace("/", ".")
			.replace(/\/(\d)/, "[$1]");

		let { message } = error;
		if (error.keyword === "enum") {
			const values = error.params.allowedValues as string[];
			message += `: ${values.join(", ")}`;
		}

		return new Error(`${dataVar}${path} ${message.replace(/"/g, "'")}`);
	}
});_
	

Expected Behavior

Compiles, resulting in pretty errors like this:
body.roles[0] must be equal to one of the allowed values: internal, owner, admin, communicator
body.roles must NOT have more than 1 items
querystring.ownerId must NOT have fewer than 28 characters

@zedeus zedeus changed the title schemaErrorFormatter uses an insufficient type schemaErrorFormatter uses an insufficient validation type Jun 29, 2022
@mcollina
Copy link
Member

This is somewhat expected as the validation library is configurable. It would be good to implement generics for this.

@mcollina mcollina added enhancement help wanted Help the community by contributing to this issue feature request New feature to be added typescript TypeScript related labels Jun 29, 2022
@Eomm Eomm added the semver-minor Issue or PR that should land as semver minor label Apr 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature to be added help wanted Help the community by contributing to this issue semver-minor Issue or PR that should land as semver minor typescript TypeScript related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants