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

Fastify ValidationResult type and AJV v8 ErrorObject mismatch #4045

Closed
2 tasks done
avin-kavish opened this issue Jun 19, 2022 · 3 comments
Closed
2 tasks done

Fastify ValidationResult type and AJV v8 ErrorObject mismatch #4045

avin-kavish opened this issue Jun 19, 2022 · 3 comments
Labels
bug Confirmed bug good first issue Good for newcomers typescript TypeScript related

Comments

@avin-kavish
Copy link

avin-kavish commented Jun 19, 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

Plugin version

No response

Node.js version

16

Operating system

Linux

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

20

Description

AJV error object is like this,

export interface ErrorObject<K extends string = string, P = Record<string, any>, S = unknown> {
  keyword: K
  instancePath: string
  schemaPath: string
  params: P
  // Added to validation errors of "propertyNames" keyword schema
  propertyName?: string
  // Excluded if option `messages` set to false.
  message?: string
  // These are added with the `verbose` option.
  schema?: S
  parentSchema?: AnySchemaObject
  data?: unknown
}

Fastify type is like this,

export interface ValidationResult {
  keyword: string;
  dataPath: string;
  schemaPath: string;
  params: Record<string, string | string[]>;
  message: string;
}

In version 3 of Fastify (and the AJV version it used), AJV errors were directly assignable to ValidationResult type and vice verca.
But in v4 the types mismatch. looks like AJV no longer has the dataPath key. It was changed here.

Steps to Reproduce

Try this in v3 and v4 projects.

import { ValidationResult } from 'fastify'
import { ErrorObject } from 'ajv'

const foo = {} as ErrorObject

const bar = {} as ValidationResult

const baz: ErrorObject = bar

const bass: ValidationResult = foo

Expected Behavior

Edit:
Maybe fastify is internally converting the ErrorObject to a ValidationError. In that case, it wouldn't be an issuue. But it maybe the case that, fastify is not handling this new ErrorObject properly.

@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@mcollina mcollina added bug Confirmed bug typescript TypeScript related good first issue Good for newcomers labels Jun 19, 2022
@avin-kavish
Copy link
Author

Adding more info for whoever picks this up, in lib/context.js it looks like defaultSchemaErrorFormatter is using instancePath so it's the type defs that are outdated.

@alex-laz
Copy link
Contributor

alex-laz commented Jun 21, 2022

Pull request: #4066

mcollina pushed a commit that referenced this issue Jun 22, 2022
* fix: fix ValidationResult interface (#4045)

* docs(typescript): update src links

for types: ValidationResult, FastifyError

* test(types): update ValidationResult inteface test

- define object properties of ajv error object

* feat(types): update ValidationResult interface

- ValidationResult extends ajv's ErrorObject

* fix(types): fix ValidationResult interface

- replace ajv interface with plain properties
@Eomm Eomm closed this as completed Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug good first issue Good for newcomers typescript TypeScript related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants