How do you validate nested request models consistently on both React and ASP.NET Core without trusting the browser?
Swiggy · Full Stack Developer · 3-4-years · technical
Published
Year not specified · .NET, React, ASP.NET Core
Community answers
Posted by wasAsked editorial collection
Validate nested input at the API boundary and enforce invariants again in the domain. Data annotations can cover simple shapes; FluentValidation or custom validators are clearer for conditional and collection rules.
Example:
RuleFor(x => x.Customer.Email).NotEmpty().EmailAddress();
RuleForEach(x => x.Lines).ChildRules(line => {
line.RuleFor(x => x.ProductId).NotEmpty();
line.RuleFor(x => x.Quantity).GreaterThan(0);
});
Return RFC 7807 validation details with stable field paths such as lines[2].quantity. Cap collection size and string length before expensive work. Validation does not replace authorization: resolve product and price server-side within the authenticated RestaurantId and OutletId, and never trust nested tenant IDs or totals supplied by the client.
Backbencher Developer · · 0 likes
The browser can be bypassed, so the server is the source of truth; client-side validation exists for fast feedback. The challenge is keeping both consistent for nested models (an order with addresses and line items).
Server (ASP.NET Core)
- Validate the whole object graph. FluentValidation handles nesting well:
public sealed class OrderValidator : AbstractValidator<CreateOrderRequest>
{
public OrderValidator()
{
RuleFor(x => x.ShippingAddress).NotNull().SetValidator(new AddressValidator());
RuleFor(x => x.Lines).NotEmpty().Must(l => l.Count <= 100);
RuleForEach(x => x.Lines).SetValidator(new OrderLineValidator());
}
}
- Return 400 Problem Details with error keys using property paths such as "lines[2].quantity" and "shippingAddress.postcode".
- Also enforce business rules that the client cannot know: stock levels, price changes, permissions.
- Reject unknown fields and limit collection sizes and string lengths to prevent abuse.
Client (React)
- Use a schema library (zod or yup) with React Hook Form, mirroring the same rules: required fields, lengths, formats, ranges.
- Map server errors back onto the form: convert "lines[2].quantity" into the form's field path and call setError, so even server-only rules appear next to the right input.
Keeping rules in sync
- Generate client types from OpenAPI, and expose validation constraints in the spec (required, maxLength, pattern, minimum) so they can drive or check client schemas.
- Keep complex or changing rules server-only and show them via the error mapping, instead of duplicating fragile logic.
- Share a test case list: the same invalid payloads are tested against the API and against the client schema.
UX tips
- Validate on blur or submit, not on every keystroke for long forms.
- Summarise errors at the top with links to fields for accessibility, and use aria-invalid and aria-describedby on inputs.
- Keep the user's input when the server rejects a submission.