GithubHelp home page GithubHelp logo

Comments (2)

sinclairzx81 avatar sinclairzx81 commented on June 17, 2024 1

@tzezar Hi,

When using Fastify, you shouldn't use Type.Date. TypeBox added this type as an extended type, but the schema it produces is unknown to Ajv (which is the validator Fastify uses internally). The issue stems from Json Schema (and Json) being unable to encode JavaScript Date objects, and that dates need to be encoded as either number or string (iso8601). For this reason, you should define the schema as either Type.Number (timestamp) or Type.String (iso8601)

Transforms

If you're using https://github.com/fastify/fastify-type-provider-typebox, unfortunately you won't be able to use Type.Transform as this is only applicable to TypeBox's validators (specifically the Encode / Decode functions). There are ways to integrate Encode/Decode into the Fastify validation pipeline, but as of today, the Encode/Decode hasn't been integrated into the Fastify type provider project (so if you want to use these Transform types, you will need to implement them manually).

From memory, you will need to override via the setValidationCompiler and preValidationHook handler. It looks like I've drafted an experimental implementation of Encode/Decode which is noted at the following issue comment.

fastify/fastify-type-provider-typebox#128 (comment)

I may take a look at providing the Type Provider project a more fleshed out implementation later in the year, but if you do try out the implementation noted on this comment thread and it works good for you, feel free to submit a PR to the provider project, ill be able to help review the PR.

Hope this helps.
S

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 17, 2024 1

@tzezar Hey, might close off this issue as it's generally related to Fastify (and Drizzle) integration.

I can only really speak for Fastify integrations (as that project I've had experience with). But as a general rule, when working with projects that are based on the Json Schema specification, only use the [Json] types and avoid the [JavaScript] types (as the latter will only work with TypeBox validators). The types listed for either category can be found here.

https://github.com/sinclairzx81/typebox?tab=readme-ov-file#types-json
https://github.com/sinclairzx81/typebox?tab=readme-ov-file#javascript-types

If you have any follow up questions, feel free to ping on this thread.
Cheers
S

from typebox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.