Skip to content

Commit

Permalink
various doc fixes (#2760)
Browse files Browse the repository at this point in the history
* Fixed typescript error in Basic Validation

* Added validator call for basic example

* added more inference to doc examples
  • Loading branch information
AlexGaudon authored Nov 15, 2024
1 parent 3810fd2 commit a4cf335
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions docs/framework/react/start/server-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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')
}

Expand All @@ -142,7 +144,6 @@ const Person = z.object({
})

export const greet = createServerFn({ method: 'GET' })
.validator(zodValidator)
.validator((person: unknown) => {
return Person.parse(person)
})
Expand Down Expand Up @@ -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')
}

Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit a4cf335

Please sign in to comment.