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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I add a custom format for a fastify's ajv instance? #732

Closed
budarin opened this issue Aug 9, 2022 · 7 comments
Closed

How do I add a custom format for a fastify's ajv instance? #732

budarin opened this issue Aug 9, 2022 · 7 comments
Labels
help wanted Extra attention is needed

Comments

@budarin
Copy link

budarin commented Aug 9, 2022

馃挰 Question here

I want to add support for extended types that are implemented in the ajv-formats package
I am trying to perform ajv initialization and describe the formats

const app = Fastify({
    ajv: {
      plugins: [require('ajv-formats']
    },
});

the scheme:

{
  "type": "object",
  "properties": {
     "id": { 
        "type" "uuid" 
     }
  }
}

but get an error:

    unknown format "uuid" ignored in schema at path "#/properties/id"

      at unknownFormat (node_modules/ajv/lib/vocabularies/format/format.ts:87:15)
      at validateFormat (node_modules/ajv/lib/vocabularies/format/format.ts:75:9)
      at Object.code (node_modules/ajv/lib/vocabularies/format/format.ts:40:10)
      at keywordCode (node_modules/ajv/lib/compile/validate/index.ts:523:9)
      at node_modules/ajv/lib/compile/validate/index.ts:265:9
      at CodeGen.code (node_modules/ajv/lib/compile/codegen/index.ts:525:33)
      at CodeGen.block (node_modules/ajv/lib/compile/codegen/index.ts:680:20)
      at iterateKeywords (node_modules/ajv/lib/compile/validate/index.ts:262:7)
      at groupKeywords (node_modules/ajv/lib/compile/validate/index.ts:241:7)
      at node_modules/ajv/lib/compile/validate/index.ts:233:38

the same result I get using this code

const app = Fastify({
    ajv: {
        customOptions: {
          formats: {
             uuid: {
               type: 'string',
               validate: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'/,
             },
          },
        },
    },
});

how can I use custom formats in fastify?

Environment

  • node version: v18.6.0
  • fastify version: ^4.4.0
  • os: any
@budarin budarin added the help wanted Extra attention is needed label Aug 9, 2022
@budarin budarin changed the title How do I add a custom format for an instance of ajv fastify ? How do I add a custom format for a fastify's ajv instance? Aug 10, 2022
@Uzlopak
Copy link

Uzlopak commented Aug 10, 2022

Well. formats are not new types. formats are formats. Maybe you should also open a issue in ajv, that they should not have an error-message unknown format "uuid" ignored in schema if in fact the type is unknown.

{
  "type": "object",
  "properties": {
     "id": { 
        "type": "string",
        "format" "uuid" 
     }
  }
}

@budarin
Copy link
Author

budarin commented Aug 10, 2022

@Uzlopak

there was a banal typo - I just missed writing {type: 'strin'} when writing the issue

with a properly designed scheme, this error is reproduced - so the question remains open

@Uzlopak
Copy link

Uzlopak commented Aug 10, 2022

I added a unit test to fastify. It seems that ajv-formats work as expected:

https://github.com/fastify/fastify/pull/4187/files

@budarin
Copy link
Author

budarin commented Aug 10, 2022

I don't understand anything - I do everything the same, but I get an error...

@budarin
Copy link
Author

budarin commented Aug 10, 2022

I have found the problem - it affected by another of ajv instance in app - I have another ajv instance to validate response from fetch to external resources and its presence somehow affects the instance in fastify

@budarin
Copy link
Author

budarin commented Aug 10, 2022

exactly the problem is in a parallel instance - if I initialize ajv-formats for it:

import Ajv from 'ajv';
import addFormats from 'ajv-formats';

const ajv = new Ajv();
addFormats(ajv);

const app = Fastify({
    ajv: {
      plugins: [addFormats]
    },
});

...

then the error does not occur, but if you do not add formats to this instance and add them to the ajv instance in fastify, an error occurs

in which package should I write issue: fastify or ajv?

@budarin
Copy link
Author

budarin commented Aug 10, 2022

it seems that my question is specific to our project and justify has no problems in ajv-formats - so I close this issue

@budarin budarin closed this as completed Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants