Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
For a Letter-Boxed–style game, validation logic should be implemented on both client and server, with different responsibilities.
1. Where to validate
- Client-side (C#, .NET MAUI, Blazor, or JavaScript/TypeScript)
- Validate quickly for UX: highlight invalid letters, disallow illegal transitions, and show immediate error messages while the user types or moves focus.
- This is analogous to validating view models or input fields in the presentation layer:
- In .NET MAUI, use a
ValidatableObject<T>-style wrapper around the user’s current word or word list, with rules like:- “All characters must be from the allowed letter set.”
- “No two consecutive letters from the same side.”
- “Word N must start with the last letter of word N-1.”
- In web UI (Razor/ASP.NET or SPA), use client-side validation helpers or JavaScript to enforce these rules as the user edits.
- In .NET MAUI, use a
- This improves usability by avoiding round trips for obvious errors.
- Server-side (.NET API, MVC, minimal APIs)
- Always revalidate submitted moves/solutions on the server before accepting them, to protect against tampering or bypassed client logic.
- Treat the game commands (e.g.,
SubmitSolutionCommand) or DTOs as input models and validate them as part of the domain logic. - This follows the guidance that even with client-side validation, server-side validation is mandatory for security and correctness.
2. Recommended design approach in .NET
- Domain model as source of truth
- Model the puzzle as a domain entity/value object (e.g.,
Puzzle,Side,WordSequence). - Implement core rules in domain services or methods:
-
bool IsValidWord(string word) -
bool IsValidTransition(string previousWord, string nextWord) -
bool IsValidSolution(IEnumerable<string> words)
-
- This keeps the rule set centralized and testable.
- Model the puzzle as a domain entity/value object (e.g.,
- View model / DTO validation for client-side
- In MVVM (e.g., .NET MAUI):
- Wrap user input in
ValidatableObject<string>orValidatableObject<IList<string>>. - Attach validation rules that call into the domain logic or reimplement the same rules for immediate feedback.
- Bind
IsValidandErrorsto the UI to highlight invalid entries and show messages.
- Wrap user input in
- In ASP.NET Web Pages / MVC / Razor:
- Use client-side validation for form fields (e.g., words list) so errors appear as soon as the user leaves a field.
- In MVVM (e.g., .NET MAUI):
3. Client-side validation patterns (web)
- For Razor/ASP.NET Web Pages:
- Use the built-in validation helpers and unobtrusive validation:
- Include jQuery and jQuery Validate + unobtrusive scripts.
- Use helpers that emit
data-val-*attributes for client-side checks.
- This gives immediate feedback while still revalidating on the server when the form is submitted.
- Use the built-in validation helpers and unobtrusive validation:
4. Security and robustness
- Treat all user input as untrusted:
- Validate and filter all string parameters and model properties on the server.
- For any API endpoints that accept primitive types or raw strings (e.g., a list of words), validate them using allow-list rules (e.g., only A–Z, max length, etc.).
- Use regular expressions carefully if used for letter/word checks, and set a regex timeout if complex patterns are involved.
5. Practical split of responsibilities
- Client-side
- Enforce syntactic and UX rules: allowed characters, no same-side adjacency, chaining between words, non-empty words, max length.
- Provide immediate visual feedback.
- Server-side
- Re-run all the above rules.
- Enforce additional game-wide rules: dictionary lookup, uniqueness of words, puzzle completion criteria, scoring.
- Persist game state and prevent cheating.
This pattern—domain model as the source of truth, with validation both in the presentation layer for UX and in the server for security and integrity—matches recommended practices for .NET applications.
References: