fix: align frontend zod schemas with backend validation rules
Tightened frontend zod schemas where backend required fields the frontend marked optional, and matched enum/format constraints. Schemas: - vehicle-document: document_type required - parts: shop_type, category, unit_type, department, sku required - inspections: customer, vehicle, department, inspection_category, employee, order_number, date, time required - appointments: service_writer required, cross-field to_time > from_time - vendor-credits: vendor + vendor_credit_date required - job-cards: documents[].document_type_id required - expense-items: category, unit_type, department, sku required - inventory-adjustments: reference_number, date required - tasks: task_type, task_section required - shop-timings: in_time, out_time enforce HH:MM:SS regex - vendor-credit: subject + vendor + vendor_credit_date required Settings: - shop-type: shop_type + note required - insurance-types: description field added, required - make-and-models: shop_type required, year 1900..2100 - departments: assignment_type required enum (AssignmentType) - company: website URL validation, latitude/longitude range, first_day_of_work enum, string max lengths - configurations: 4 fields enum-typed (was raw strings) Inline forms: - job-card service/part/expense-item: relations required (part/service/expense_item/department) - job-card recommendation: max 255 - vehicles/inline-forms/shop-type: shop_type + note required - vehicles/inline-forms/body-type: shop_type_id pulled from parent form context, guard on submit - vehicles/inline-forms/color: code field added, required - services/inline-forms/department: assignment_type required enum - tasks/task-section: arrangement required integer - invoices/invoice-edit: status + discount enum-typed Auth: - login: password min 6 (was 8; backend allows 6) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
349a458c3c
commit
cc7dc1bd17
@@ -1,7 +1,7 @@
|
||||
"use client"
|
||||
|
||||
import { z } from "zod"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { useForm, useFormContext } from "react-hook-form"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { Plus } from "lucide-react"
|
||||
import { Button } from "@/shared/components/ui/button"
|
||||
@@ -11,13 +11,14 @@ import { toast } from "sonner"
|
||||
import { useAuthApi } from "@/shared/useApi"
|
||||
|
||||
const schema = z.object({
|
||||
title: z.string().min(1, "Title is required"),
|
||||
title: z.string().min(1, "Title is required").max(50, "Title cannot exceed 50 characters"),
|
||||
})
|
||||
|
||||
type FormValues = z.infer<typeof schema>
|
||||
|
||||
export function BodyTypeInlineForm({ onSuccess }: InlineCreateFormProps) {
|
||||
const api = useAuthApi()
|
||||
const parentForm = useFormContext()
|
||||
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(schema),
|
||||
@@ -25,8 +26,16 @@ export function BodyTypeInlineForm({ onSuccess }: InlineCreateFormProps) {
|
||||
})
|
||||
|
||||
const handleSubmit = async (values: FormValues) => {
|
||||
const parentShopType = parentForm?.getValues("shop_type_id") as { value?: string } | null | undefined
|
||||
const shopTypeId = parentShopType?.value ? Number(parentShopType.value) : undefined
|
||||
|
||||
if (!shopTypeId) {
|
||||
toast.error("Select a shop type before creating a body type")
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await api.vehicleAttributes.createBodyType({ title: values.title })
|
||||
const result = await api.vehicleAttributes.createBodyType({ title: values.title, shop_type_id: shopTypeId } as any)
|
||||
toast.success("Body type created")
|
||||
form.reset()
|
||||
const item = (result as any)?.data ?? result
|
||||
|
||||
@@ -11,7 +11,8 @@ import { toast } from "sonner"
|
||||
import { useAuthApi } from "@/shared/useApi"
|
||||
|
||||
const schema = z.object({
|
||||
title: z.string().min(1, "Title is required"),
|
||||
title: z.string().min(1, "Title is required").max(50, "Title cannot exceed 50 characters"),
|
||||
code: z.string().min(1, "Code is required").max(50, "Code cannot exceed 50 characters"),
|
||||
})
|
||||
|
||||
type FormValues = z.infer<typeof schema>
|
||||
@@ -21,12 +22,12 @@ export function ColorInlineForm({ onSuccess }: InlineCreateFormProps) {
|
||||
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: { title: "" },
|
||||
defaultValues: { title: "", code: "" },
|
||||
})
|
||||
|
||||
const handleSubmit = async (values: FormValues) => {
|
||||
try {
|
||||
const result = await api.vehicleAttributes.createColor({ title: values.title })
|
||||
const result = await api.vehicleAttributes.createColor({ title: values.title, code: values.code } as any)
|
||||
toast.success("Color created")
|
||||
form.reset()
|
||||
const item = (result as any)?.data ?? result
|
||||
@@ -45,6 +46,12 @@ export function ColorInlineForm({ onSuccess }: InlineCreateFormProps) {
|
||||
placeholder="e.g. Black"
|
||||
required
|
||||
/>
|
||||
<RhfTextField
|
||||
name="code"
|
||||
label="Code"
|
||||
placeholder="e.g. #000000"
|
||||
required
|
||||
/>
|
||||
<Button type="submit" disabled={form.formState.isSubmitting}>
|
||||
<Plus />
|
||||
{form.formState.isSubmitting ? "Creating..." : "Create Color"}
|
||||
|
||||
@@ -18,9 +18,9 @@ import { toast } from "sonner"
|
||||
import { useAuthApi } from "@/shared/useApi"
|
||||
|
||||
const schema = z.object({
|
||||
title: z.string().min(1, "Title is required"),
|
||||
shop_type: z.string().optional(),
|
||||
note: z.string().optional(),
|
||||
title: z.string().min(1, "Title is required").max(50, "Title cannot exceed 50 characters"),
|
||||
shop_type: z.string().min(1, "Shop type is required"),
|
||||
note: z.string().min(1, "Note is required").max(255, "Note cannot exceed 255 characters"),
|
||||
is_default: z.boolean().optional(),
|
||||
inspection: z.any().optional(),
|
||||
image: z.any().optional(),
|
||||
@@ -28,8 +28,8 @@ const schema = z.object({
|
||||
|
||||
type FormValues = {
|
||||
title: string
|
||||
shop_type?: string
|
||||
note?: string
|
||||
shop_type: string
|
||||
note: string
|
||||
is_default?: boolean
|
||||
inspection?: File | null
|
||||
image?: File | null
|
||||
|
||||
@@ -3,7 +3,7 @@ import { z } from "zod"
|
||||
const relationFieldSchema = z.object({ value: z.string(), label: z.string() }).nullable()
|
||||
|
||||
export const vehicleDocumentFormSchema = z.object({
|
||||
document_type: relationFieldSchema,
|
||||
document_type: relationFieldSchema.refine((val) => !!val?.value, "Document type is required"),
|
||||
customer: relationFieldSchema,
|
||||
document_number: z.string().optional(),
|
||||
document_expire: z.string().optional(),
|
||||
|
||||
Reference in New Issue
Block a user