From 5e3d52564f335373211736e7104e2c988e72583a Mon Sep 17 00:00:00 2001 From: Elton Lobo Date: Sat, 30 Nov 2024 23:08:22 +0530 Subject: [PATCH 1/2] improve `optionals` guide Improvements: - Replace `Input` and `Output` with `InferInput` and `InferOutput` in one of the examples - Change the behavior of the `Dependent default values` example to enable TypeScript to infer the correct output type --- .../routes/guides/(schemas)/optionals/index.mdx | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/website/src/routes/guides/(schemas)/optionals/index.mdx b/website/src/routes/guides/(schemas)/optionals/index.mdx index 4d43d00c6..0fd2fc6fc 100644 --- a/website/src/routes/guides/(schemas)/optionals/index.mdx +++ b/website/src/routes/guides/(schemas)/optionals/index.mdx @@ -7,6 +7,7 @@ description: >- contributors: - fabian-hiller - fartinmartin + - EltonLobo07 --- import { Link } from '@builder.io/qwik-city'; @@ -47,8 +48,8 @@ import * as v from 'valibot'; const OptionalStringSchema = v.optional(v.string(), "I'm the default!"); -type OptionalStringInput = v.Input; // string | undefined -type OptionalStringOutput = v.Output; // string +type OptionalStringInput = v.InferInput; // string | undefined +type OptionalStringOutput = v.InferOutput; // string ``` By providing a default value, the input type of the schema now differs from the output type. The schema in the example now accepts `string` and `undefined` as input, but returns a string as output in both cases. @@ -70,6 +71,8 @@ The previous example thus creates a new instance of the [`Date`](https://develop In rare cases, a default value for an optional entry may depend on the values of another entries in the same object. This can be achieved by using `transform` in the `pipe` of the object. ```ts +import * as v from 'valibot'; + const CalculationSchema = v.pipe( v.object({ a: v.number(), @@ -77,10 +80,10 @@ const CalculationSchema = v.pipe( sum: v.optional(v.number()), }), v.transform((input) => { - if (input.sum === undefined) { - return { ...input, sum: input.a + input.b }; - } - return input; + return { + ...input, + sum: input.sum === undefined ? input.a + input.b : input.sum, + }; }) ); ``` From bbdbc90ec80cda711e39e55336db6d66d4fd92e5 Mon Sep 17 00:00:00 2001 From: Elton Lobo Date: Thu, 5 Dec 2024 01:21:51 +0530 Subject: [PATCH 2/2] simplify `optionals` guide's 'Dependent default values' example --- .../src/routes/guides/(schemas)/optionals/index.mdx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/website/src/routes/guides/(schemas)/optionals/index.mdx b/website/src/routes/guides/(schemas)/optionals/index.mdx index 0fd2fc6fc..0c0a00a4f 100644 --- a/website/src/routes/guides/(schemas)/optionals/index.mdx +++ b/website/src/routes/guides/(schemas)/optionals/index.mdx @@ -79,11 +79,9 @@ const CalculationSchema = v.pipe( b: v.number(), sum: v.optional(v.number()), }), - v.transform((input) => { - return { - ...input, - sum: input.sum === undefined ? input.a + input.b : input.sum, - }; - }) + v.transform((input) => ({ + ...input, + sum: input.sum === undefined ? input.a + input.b : input.sum, + })) ); ```