fastify/fast-json-stringify

Issue with custom keyword not working in fast-json-stringify

RumyantsevOleg opened this issue ยท 11 comments

Prerequisites

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

Fastify version

without fastify

Plugin version

5.8.0

Node.js version

20.7.0

Operating system

macOS

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

14.1

Description

Description:
I'm encountering an issue when using the fast-json-stringify library for data serialization. I use it without Fastify. I've successfully implemented validation using Ajv, including a custom keyword. However, when passing the custom keyword to fast-json-stringify, it doesn't seem to work as expected.

Upon reviewing the type annotations, it appears that Ajv is a complete configuration object, but the custom keyword is not functioning correctly when used with fast-json-stringify.

I would appreciate any guidance or assistance in resolving this issue.

Steps to Reproduce

You can pass in your own custom keyword and see that it doesn't work

const serialization = fastJson(schema, {
   ajv: {
      keywords: [
         {
            keyword: 'mask',
            modifying: true,
            type: 'string',
            validate: () => {
               console.log('validate')
            },
            code: () => {
               console.log('code')
            },
            compile: () => {
               console.log('compile')
            },
            errors: false
         }
      ]
   }
})

Expected Behavior

The expected behavior is that the custom keyword will work in fast-json-stringify

What would you expect?

I expect the "custom keywords" to work the same way they do in ajv. But it doesn't.

I have a need to extend the capabilities of json schemas and have implemented this through custom keywords, but as far as I can see the fast-json-stringify library does not support this functionality.

Ajv is a validator. fast-json-stringifty is a serializer. They can not work in the "same way".

I understand, but I mean validation before serialization

You can call an ajv.validate before serialization.

Yes it is an option, you mean create your own ajv instance and call it? But wouldn't that result in double checking?

fast-json-stringify doesn't validate your data by default. It uses an ajv only in special cases when it needs to find a correct "path" in your schema: oneOf, anyOf, if/then/else keywords. In all other cases it doens't validate your data.

Thank you. Just to clarify. For example, if some field is marked as required in the schema, but it is not in the passed values or if there is a type mismatch when serializing fast-json-stringify will generate an error. And fast-json-stringify accepts ajv config. Which led me to the logical conclusion that validation must be present. Do you know if there are any out of the box ways for fast-json-stringify to transform the data? And is there any note on how ajv is specifically used? Also do you think it is quite normal practice to perform validation before sterilization? Thanks

  1. fast-json-stringify does some it's own checks in some places to prevent a security issues, but you shouldn't rely in it. If you don't trust your data you should validate it with a validator like ajv.
  2. fast-json-stringify accepts ajv config because it uses an ajv internally, but it doesn't mean that it validate your whole schema every time.
  3. fast-json-stringify supports type coersing. It doesn't have any option for custom data transormation during the serialization process.
  4. https://github.com/fastify/fast-json-stringify#anyof-and-oneof
  5. If you need to validate your data before serialization, you can ... ๐Ÿฅ๐Ÿฅ๐Ÿฅ ... validate your data before serialization.

Thanks, " ๐Ÿฅ๐Ÿฅ๐Ÿฅ" made me laugh ๐Ÿ˜ƒ