From a4cf33539273678c0b7dbfc73eec08a5d8d59954 Mon Sep 17 00:00:00 2001 From: AlexGaudon <35076791+AlexGaudon@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:44:53 -0330 Subject: [PATCH] various doc fixes (#2760) * Fixed typescript error in Basic Validation * Added validator call for basic example * added more inference to doc examples --- .../framework/react/start/server-functions.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/docs/framework/react/start/server-functions.md b/docs/framework/react/start/server-functions.md index 16d590956e..cdca2cbcae 100644 --- a/docs/framework/react/start/server-functions.md +++ b/docs/framework/react/start/server-functions.md @@ -81,9 +81,11 @@ import { createServerFn } from '@tanstack/start' export const greet = createServerFn({ method: 'GET', -}).handler(async (ctx) => { - return `Hello, ${ctx.data}!` }) + .validator((data: string) => data) + .handler(async (ctx) => { + return `Hello, ${ctx.data}!` + }) greet({ data: 'John', @@ -117,7 +119,7 @@ export const greet = createServerFn({ method: 'GET' }) throw new Error('Person must be an object') } - if (typeof person.name !== 'string') { + if ('name' in person && typeof person.name !== 'string') { throw new Error('Person.name must be a string') } @@ -142,7 +144,6 @@ const Person = z.object({ }) export const greet = createServerFn({ method: 'GET' }) - .validator(zodValidator) .validator((person: unknown) => { return Person.parse(person) }) @@ -174,7 +175,7 @@ export const greet = createServerFn({ method: 'GET' }) throw new Error('Person must be an object') } - if (typeof person.name !== 'string') { + if ('name' in person && typeof person.name !== 'string') { throw new Error('Person.name must be a string') } @@ -257,11 +258,11 @@ type Person = { age: number } -export const greet = createServerFn({ method: 'GET' }).handler( - async ({ data }) => { +export const greet = createServerFn({ method: 'GET' }) + .validator((data) => data) + .handler(async ({ data }) => { return `Hello, ${data.name}! You are ${data.age} years old.` - }, -) + }) greet({ data: {