You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// The `x`, `y`, and `z` types are automatically inferred
58
+
// The `x`, `y`, and `z` types are automatically inferred
60
59
const { x, y, z } =req.body;
61
60
}
62
61
);
63
62
};
64
63
```
65
64
66
-
## Using References from a Shared Schema
65
+
## Setting FromSchema for the validator and serializer
67
66
68
-
JsonSchemaToTsProvider takes a generic that can be passed in the Shared Schema
69
-
as shown in the following example
67
+
You can set the `FromSchema` settings for things like [`references`](https://github.com/ThomasAribart/json-schema-to-ts#references) and [`deserialization`](https://github.com/ThomasAribart/json-schema-to-ts#deserialization) for the validation and serialization schema by setting `ValidatorSchemaOptions` and `SerializerSchemaOptions` type parameters.
68
+
You can use the `deserialize` option in `SerializerSchemaOptions` to allow Date objects in place of date-time strings or other special serialization rules handled by [fast-json-stringify](https://github.com/fastify/fast-json-stringify?tab=readme-ov-file#specific-use-cases).
// now reference the shared schema like the following
97
126
fastify.get(
98
127
"/profile",
99
128
{
@@ -107,11 +136,128 @@ fastify.get(
107
136
},
108
137
required: ['user'],
109
138
},
139
+
response: {
140
+
200: { $ref: "userProfile#" },
141
+
},
110
142
} asconst,
111
143
},
112
-
(req) => {
113
-
// givenName and familyName will be correctly typed as strings!
144
+
(req, reply) => {
145
+
//`givenName` and `familyName` are correctly typed as strings
114
146
const { givenName, familyName } =req.body.user;
147
+
148
+
// Construct a compatible response type
149
+
const profile:UserProfile= {
150
+
user: { givenName: "John", familyName: "Doe" },
151
+
joinedAt: newDate(), // Returning a Date object
152
+
};
153
+
154
+
// A type error is surfaced if profile doesn't match the serialization schema
155
+
reply.send(profile)
115
156
}
116
-
);
157
+
)
158
+
```
159
+
160
+
## Using References in a Plugin Definition
161
+
162
+
When defining a plugin, shared schema references and deserialization options can also be used with `FastifyPluginAsyncJsonSchemaToTs` and `FastifyPluginCallbackJsonSchemaToTs`.
0 commit comments