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,27 +1,53 @@
|
||||
import { z } from "zod"
|
||||
import { FirstDayOfWork } from "@garage/api"
|
||||
|
||||
export const relationFieldSchema = z
|
||||
.object({ value: z.string(), label: z.string() })
|
||||
.nullable()
|
||||
|
||||
const optionalLatitude = z
|
||||
.string()
|
||||
.optional()
|
||||
.refine(
|
||||
(v) => v == null || v === "" || (!Number.isNaN(Number(v)) && Number(v) >= -90 && Number(v) <= 90),
|
||||
"Latitude must be between -90 and 90",
|
||||
)
|
||||
|
||||
const optionalLongitude = z
|
||||
.string()
|
||||
.optional()
|
||||
.refine(
|
||||
(v) => v == null || v === "" || (!Number.isNaN(Number(v)) && Number(v) >= -180 && Number(v) <= 180),
|
||||
"Longitude must be between -180 and 180",
|
||||
)
|
||||
|
||||
const optionalUrl = z
|
||||
.string()
|
||||
.max(255, "Website cannot exceed 255 characters")
|
||||
.optional()
|
||||
.refine(
|
||||
(v) => v == null || v === "" || z.string().url().safeParse(v).success,
|
||||
"Enter a valid website URL",
|
||||
)
|
||||
|
||||
export const settingsFormSchema = z.object({
|
||||
name: z.string().min(1, "Name is required"),
|
||||
name: z.string().min(1, "Name is required").max(100, "Name cannot exceed 100 characters"),
|
||||
email: z.union([z.string().email("Invalid email address"), z.literal("")]).optional(),
|
||||
phone: z.string().optional(),
|
||||
alternative_phone: z.string().optional(),
|
||||
website: z.string().optional(),
|
||||
time_zone: z.string().optional(),
|
||||
upi_id: z.string().optional(),
|
||||
first_day_of_work: z.string().optional(),
|
||||
latitude: z.string().optional(),
|
||||
longitude: z.string().optional(),
|
||||
phone: z.string().max(30, "Phone cannot exceed 30 characters").optional(),
|
||||
alternative_phone: z.string().max(30, "Phone cannot exceed 30 characters").optional(),
|
||||
website: optionalUrl,
|
||||
time_zone: z.string().max(100, "Time zone cannot exceed 100 characters").optional(),
|
||||
upi_id: z.string().max(100, "UPI cannot exceed 100 characters").optional(),
|
||||
first_day_of_work: z.union([z.enum(FirstDayOfWork), z.literal("")]).optional(),
|
||||
latitude: optionalLatitude,
|
||||
longitude: optionalLongitude,
|
||||
bank_details: z.string().optional(),
|
||||
first_address_line: z.string().optional(),
|
||||
second_address_line: z.string().optional(),
|
||||
first_address_line: z.string().max(255, "Address cannot exceed 255 characters").optional(),
|
||||
second_address_line: z.string().max(255, "Address cannot exceed 255 characters").optional(),
|
||||
country: relationFieldSchema,
|
||||
state: relationFieldSchema,
|
||||
city: z.string().optional(),
|
||||
zip_code: z.string().optional(),
|
||||
city: z.string().max(100, "City cannot exceed 100 characters").optional(),
|
||||
zip_code: z.string().max(20, "Zip code cannot exceed 20 characters").optional(),
|
||||
description: z.string().optional(),
|
||||
security: z.string().optional(),
|
||||
privacy_policy: z.string().optional(),
|
||||
|
||||
@@ -1,24 +1,30 @@
|
||||
import { z } from "zod"
|
||||
import {
|
||||
SellRatesTaxInclusive,
|
||||
GiveDiscounts,
|
||||
PurchaseRatesTaxInclusive,
|
||||
ReceiveDiscounts,
|
||||
} from "@garage/api"
|
||||
|
||||
export const salesConfigFormSchema = z.object({
|
||||
sell_rates_tax_inclusive: z.string().optional(),
|
||||
give_discounts: z.string().optional(),
|
||||
sell_rates_tax_inclusive: z.enum(SellRatesTaxInclusive).optional(),
|
||||
give_discounts: z.enum(GiveDiscounts).optional(),
|
||||
})
|
||||
|
||||
export type SalesConfigFormValues = z.infer<typeof salesConfigFormSchema>
|
||||
|
||||
export const purchaseConfigFormSchema = z.object({
|
||||
purchase_rates_tax_inclusive: z.string().optional(),
|
||||
receive_discounts: z.string().optional(),
|
||||
purchase_rates_tax_inclusive: z.enum(PurchaseRatesTaxInclusive).optional(),
|
||||
receive_discounts: z.enum(ReceiveDiscounts).optional(),
|
||||
})
|
||||
|
||||
export type PurchaseConfigFormValues = z.infer<typeof purchaseConfigFormSchema>
|
||||
|
||||
export const generalPreferencesFormSchema = z.object({
|
||||
sell_rates_tax_inclusive: z.string().optional(),
|
||||
give_discounts: z.string().optional(),
|
||||
purchase_rates_tax_inclusive: z.string().optional(),
|
||||
receive_discounts: z.string().optional(),
|
||||
sell_rates_tax_inclusive: z.enum(SellRatesTaxInclusive).optional(),
|
||||
give_discounts: z.enum(GiveDiscounts).optional(),
|
||||
purchase_rates_tax_inclusive: z.enum(PurchaseRatesTaxInclusive).optional(),
|
||||
receive_discounts: z.enum(ReceiveDiscounts).optional(),
|
||||
})
|
||||
|
||||
export type GeneralPreferencesFormValues = z.infer<typeof generalPreferencesFormSchema>
|
||||
|
||||
@@ -23,10 +23,10 @@ const DISCOUNT_OPTIONS = DiscountType.map((v) => ({
|
||||
}))
|
||||
|
||||
const DEFAULT_VALUES: GeneralPreferencesFormValues = {
|
||||
sell_rates_tax_inclusive: "",
|
||||
give_discounts: "",
|
||||
purchase_rates_tax_inclusive: "",
|
||||
receive_discounts: "",
|
||||
sell_rates_tax_inclusive: undefined,
|
||||
give_discounts: undefined,
|
||||
purchase_rates_tax_inclusive: undefined,
|
||||
receive_discounts: undefined,
|
||||
}
|
||||
|
||||
export function GeneralPreferencesForm() {
|
||||
|
||||
@@ -23,8 +23,8 @@ const DISCOUNT_OPTIONS = DiscountType.map((v) => ({
|
||||
}))
|
||||
|
||||
const DEFAULT_VALUES: PurchaseConfigFormValues = {
|
||||
purchase_rates_tax_inclusive: "",
|
||||
receive_discounts: "",
|
||||
purchase_rates_tax_inclusive: undefined,
|
||||
receive_discounts: undefined,
|
||||
}
|
||||
|
||||
export function PurchaseConfigForm() {
|
||||
|
||||
@@ -23,8 +23,8 @@ const DISCOUNT_OPTIONS = DiscountType.map((v) => ({
|
||||
}))
|
||||
|
||||
const DEFAULT_VALUES: SalesConfigFormValues = {
|
||||
sell_rates_tax_inclusive: "",
|
||||
give_discounts: "",
|
||||
sell_rates_tax_inclusive: undefined,
|
||||
give_discounts: undefined,
|
||||
}
|
||||
|
||||
export function SalesConfigForm() {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { z } from "zod"
|
||||
import { AssignmentType } from "@garage/api"
|
||||
|
||||
export const departmentFormSchema = z.object({
|
||||
name: z.string().min(1, "Name is required"),
|
||||
assignment_type: z.string().optional(),
|
||||
name: z.string().min(1, "Name is required").max(50, "Name cannot exceed 50 characters"),
|
||||
assignment_type: z.enum(AssignmentType, { error: "Assignment type is required" }),
|
||||
})
|
||||
|
||||
export type DepartmentFormValues = z.infer<typeof departmentFormSchema>
|
||||
@@ -22,18 +22,21 @@ export type InsuranceTypeFormProps = {
|
||||
|
||||
const DEFAULT_VALUES: InsuranceTypeFormValues = {
|
||||
title: "",
|
||||
description: "",
|
||||
}
|
||||
|
||||
function mapToFormValues(data: unknown): InsuranceTypeFormValues {
|
||||
const d = (data as any)?.data ?? data ?? {}
|
||||
return {
|
||||
title: d.title ?? d.name ?? "",
|
||||
description: d.description ?? "",
|
||||
}
|
||||
}
|
||||
|
||||
function mapFormToPayload(values: InsuranceTypeFormValues) {
|
||||
return {
|
||||
title: values.title,
|
||||
description: values.description,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +93,13 @@ export function InsuranceTypeForm({ resourceId, initialData, onSuccess }: Insura
|
||||
required
|
||||
/>
|
||||
|
||||
<RhfTextField
|
||||
name="description"
|
||||
label="Description"
|
||||
placeholder="Short description"
|
||||
required
|
||||
/>
|
||||
|
||||
<Button type="submit" variant="default" disabled={isPending}>
|
||||
{isEditing ? <Save /> : <Plus />}
|
||||
{isPending
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const insuranceTypeFormSchema = z.object({
|
||||
title: z.string().min(1, "Title is required"),
|
||||
title: z.string().min(1, "Title is required").max(255, "Title cannot exceed 255 characters"),
|
||||
description: z.string().min(1, "Description is required").max(255, "Description cannot exceed 255 characters"),
|
||||
})
|
||||
|
||||
export type InsuranceTypeFormValues = z.infer<typeof insuranceTypeFormSchema>
|
||||
@@ -3,13 +3,19 @@ import { z } from "zod"
|
||||
const relationFieldSchema = z.object({ value: z.string(), label: z.string() }).nullable()
|
||||
|
||||
export const makeAndModelFormSchema = z.object({
|
||||
make: z.string().min(1, "Make is required"),
|
||||
model: z.string().min(1, "Model is required"),
|
||||
year: z.string().optional(),
|
||||
make: z.string().min(1, "Make is required").max(255, "Make cannot exceed 255 characters"),
|
||||
model: z.string().min(1, "Model is required").max(255, "Model cannot exceed 255 characters"),
|
||||
year: z
|
||||
.string()
|
||||
.min(1, "Year is required")
|
||||
.refine((v) => {
|
||||
const n = Number(v)
|
||||
return Number.isInteger(n) && n >= 1900 && n <= 2100
|
||||
}, "Year must be a whole number between 1900 and 2100"),
|
||||
sub_model: z.string().optional(),
|
||||
engine_size: z.string().optional(),
|
||||
drivetrain: z.string().optional(),
|
||||
shop_type: relationFieldSchema,
|
||||
shop_type: relationFieldSchema.refine((val) => !!val?.value, "Shop type is required"),
|
||||
body_type: relationFieldSchema,
|
||||
fuel_type: relationFieldSchema,
|
||||
transmission: relationFieldSchema,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const shopTypeFormSchema = 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(),
|
||||
|
||||
Reference in New Issue
Block a user