Skip to content

Add patch for validation error types on inertia:install #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/generators/inertia/install/frameworks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ react:
"tsconfig.app.json": "tsconfig.app.json"
"tsconfig.node.json": "tsconfig.node.json"
"vite-env.d.ts": "%{js_destination_path}/vite-env.d.ts"
"inertia-rails.d.ts": "%{js_destination_path}/types/inertia-rails.d.ts"
copy_files_js:
"InertiaExample.jsx": "%{js_destination_path}/pages/InertiaExample.jsx"
copy_files:
Expand Down Expand Up @@ -44,6 +45,7 @@ vue:
"tsconfig.app.json": "tsconfig.app.json"
"tsconfig.node.json": "tsconfig.node.json"
"vite-env.d.ts": "%{js_destination_path}/vite-env.d.ts"
"inertia-rails.d.ts": "%{js_destination_path}/types/inertia-rails.d.ts"
copy_files_js:
"InertiaExample.vue": "%{js_destination_path}/pages/InertiaExample.vue"

Expand All @@ -64,6 +66,7 @@ svelte4:
"tsconfig.json": "tsconfig.json"
"tsconfig.node.json": "tsconfig.node.json"
"vite-env.d.ts": "%{js_destination_path}/vite-env.d.ts"
"inertia-rails.d.ts": "%{js_destination_path}/types/inertia-rails.d.ts"
copy_files_js:
"InertiaExample.svelte": "%{js_destination_path}/pages/InertiaExample.svelte"
copy_files:
Expand All @@ -89,6 +92,7 @@ svelte:
"tsconfig.json": "tsconfig.json"
"tsconfig.node.json": "tsconfig.node.json"
"vite-env.d.ts": "%{js_destination_path}/vite-env.d.ts"
"inertia-rails.d.ts": "%{js_destination_path}/types/inertia-rails.d.ts"
copy_files_js:
"InertiaExample.svelte": "%{js_destination_path}/pages/InertiaExample.svelte"
copy_files:
Expand Down
26 changes: 26 additions & 0 deletions lib/generators/inertia/install/templates/react/inertia-rails.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// See https://inertia-rails.dev/cookbook/handling-validation-error-types.md
import type { FormDataConvertible, FormDataKeys } from '@inertiajs/core'
import type { InertiaFormProps as OriginalProps } from '@inertiajs/react'

type FormDataType = Record<string, FormDataConvertible>

declare module '@inertiajs/react' {
interface InertiaFormProps<TForm extends FormDataType>
extends Omit<OriginalProps<TForm>, 'errors' | 'setError'> {
errors: Partial<Record<FormDataKeys<TForm>, string[]>>

setError(field: FormDataKeys<TForm>, value: string[]): void

setError(errors: Record<FormDataKeys<TForm>, string[]>): void
}

export { InertiaFormProps }

export function useForm<TForm extends FormDataType>(
initialValues?: TForm,
): InertiaFormProps<TForm>
export function useForm<TForm extends FormDataType>(
rememberKey: string,
initialValues?: TForm,
): InertiaFormProps<TForm>
}
29 changes: 29 additions & 0 deletions lib/generators/inertia/install/templates/svelte/inertia-rails.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// See https://inertia-rails.dev/cookbook/handling-validation-error-types.md
import type { FormDataConvertible, FormDataKeys } from '@inertiajs/core'
import type { InertiaFormProps as OriginalProps } from '@inertiajs/svelte'
import type { Writable } from 'svelte/store'

type FormDataType = Record<string, FormDataConvertible>

declare module '@inertiajs/svelte' {
interface InertiaFormProps<TForm extends FormDataType>
extends Omit<OriginalProps<TForm>, 'errors' | 'setError'> {
errors: Partial<Record<FormDataKeys<TForm>, string[]>>

setError(field: FormDataKeys<TForm>, value: string[]): this

setError(errors: Record<FormDataKeys<TForm>, string[]>): this
}

type InertiaForm<TForm extends FormDataType> = InertiaFormProps<TForm> & TForm

export { InertiaFormProps, InertiaForm }

export function useForm<TForm extends FormDataType>(
data: TForm | (() => TForm),
): Writable<InertiaForm<TForm>>
export function useForm<TForm extends FormDataType>(
rememberKey: string,
data: TForm | (() => TForm),
): Writable<InertiaForm<TForm>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// See https://inertia-rails.dev/cookbook/handling-validation-error-types.md
import type { FormDataConvertible, FormDataKeys } from '@inertiajs/core'
import type { InertiaFormProps as OriginalProps } from '@inertiajs/svelte'
import type { Writable } from 'svelte/store'

type FormDataType = Record<string, FormDataConvertible>

declare module '@inertiajs/svelte' {
interface InertiaFormProps<TForm extends FormDataType>
extends Omit<OriginalProps<TForm>, 'errors' | 'setError'> {
errors: Partial<Record<FormDataKeys<TForm>, string[]>>

setError(field: FormDataKeys<TForm>, value: string[]): this

setError(errors: Record<FormDataKeys<TForm>, string[]>): this
}

type InertiaForm<TForm extends FormDataType> = InertiaFormProps<TForm> & TForm

export { InertiaFormProps, InertiaForm }

export function useForm<TForm extends FormDataType>(
data: TForm | (() => TForm),
): Writable<InertiaForm<TForm>>
export function useForm<TForm extends FormDataType>(
rememberKey: string,
data: TForm | (() => TForm),
): Writable<InertiaForm<TForm>>
}
29 changes: 29 additions & 0 deletions lib/generators/inertia/install/templates/vue/inertia-rails.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// See https://inertia-rails.dev/cookbook/handling-validation-error-types.md
import type { FormDataConvertible, FormDataKeys } from '@inertiajs/core'
import type { InertiaFormProps as OriginalProps } from '@inertiajs/vue3'

type FormDataType = Record<string, FormDataConvertible>

declare module '@inertiajs/vue3' {
interface InertiaFormProps<TForm extends FormDataType>
extends Omit<OriginalProps<TForm>, 'errors' | 'setError'> {
errors: Partial<Record<FormDataKeys<TForm>, string[]>>

setError(field: FormDataKeys<TForm>, value: string[]): this

setError(errors: Record<FormDataKeys<TForm>, string[]>): this
}

export type InertiaForm<TForm extends FormDataType> = TForm &
InertiaFormProps<TForm>

export { InertiaFormProps, InertiaForm }

export function useForm<TForm extends FormDataType>(
data: TForm | (() => TForm),
): InertiaForm<TForm>
export function useForm<TForm extends FormDataType>(
rememberKey: string,
data: TForm | (() => TForm),
): InertiaForm<TForm>
}
3 changes: 3 additions & 0 deletions spec/generators/install/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ def expect_inertia_prepared_for(framework, ext: 'js')
file('app/frontend/vite-env.d.ts') do
contains('/// <reference types="vite/client" />')
end
directory('app/frontend/types') do
file('inertia-rails.d.ts')
end
file('tsconfig.node.json') do
contains('"include": ["vite.config.ts"]')
end
Expand Down