page.mdx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. export const metadata = { title: "Validation" }
  2. # Validation
  3. Validate form inputs with built-in and custom functions.
  4. ## Built-in Validators
  5. json-render includes common validation functions:
  6. - `required` — Value must be non-empty
  7. - `email` — Valid email format
  8. - `minLength` — Minimum string length
  9. - `maxLength` — Maximum string length
  10. - `pattern` — Match a regex pattern
  11. - `min` — Minimum numeric value
  12. - `max` — Maximum numeric value
  13. ## Using Validation in JSON
  14. ```json
  15. {
  16. "type": "TextField",
  17. "props": {
  18. "label": "Email",
  19. "valuePath": "/form/email",
  20. "checks": [
  21. { "fn": "required", "message": "Email is required" },
  22. { "fn": "email", "message": "Invalid email format" }
  23. ],
  24. "validateOn": "blur"
  25. }
  26. }
  27. ```
  28. ## Validation with Parameters
  29. ```json
  30. {
  31. "type": "TextField",
  32. "props": {
  33. "label": "Password",
  34. "valuePath": "/form/password",
  35. "checks": [
  36. { "fn": "required", "message": "Password is required" },
  37. {
  38. "fn": "minLength",
  39. "args": { "length": 8 },
  40. "message": "Password must be at least 8 characters"
  41. },
  42. {
  43. "fn": "pattern",
  44. "args": { "pattern": "[A-Z]" },
  45. "message": "Must contain at least one uppercase letter"
  46. }
  47. ]
  48. }
  49. }
  50. ```
  51. ## Custom Validation Functions
  52. Define custom validators in your catalog:
  53. ```typescript
  54. import { defineCatalog } from '@json-render/core';
  55. import { schema } from '@json-render/react';
  56. import { z } from 'zod';
  57. const catalog = defineCatalog(schema, {
  58. components: { /* ... */ },
  59. functions: {
  60. isValidPhone: {
  61. description: 'Validates phone number format',
  62. },
  63. isUniqueEmail: {
  64. description: 'Checks if email is not already registered',
  65. },
  66. },
  67. });
  68. ```
  69. Then implement them in your ValidationProvider:
  70. ```tsx
  71. import { ValidationProvider } from '@json-render/react';
  72. function App() {
  73. const customValidators = {
  74. isValidPhone: (value) => {
  75. const phoneRegex = /^\+?[1-9]\d{1,14}$/;
  76. return phoneRegex.test(value);
  77. },
  78. isUniqueEmail: async (value) => {
  79. const response = await fetch(`/api/check-email?email=${value}`);
  80. const { available } = await response.json();
  81. return available;
  82. },
  83. };
  84. return (
  85. <ValidationProvider functions={customValidators}>
  86. {/* Your UI */}
  87. </ValidationProvider>
  88. );
  89. }
  90. ```
  91. ## Using in Components
  92. ```tsx
  93. import { useFieldValidation } from '@json-render/react';
  94. function TextField({ props }) {
  95. const { value, setValue, errors, validate } = useFieldValidation(
  96. props.valuePath,
  97. props.checks
  98. );
  99. return (
  100. <div>
  101. <label>{props.label}</label>
  102. <input
  103. value={value || ''}
  104. onChange={(e) => setValue(e.target.value)}
  105. onBlur={() => validate()}
  106. />
  107. {errors.map((error, i) => (
  108. <p key={i} className="text-red-500 text-sm">{error}</p>
  109. ))}
  110. </div>
  111. );
  112. }
  113. ```
  114. ## Validation Timing
  115. Control when validation runs with `validateOn`:
  116. - `change` — Validate on every input change
  117. - `blur` — Validate when field loses focus
  118. - `submit` — Validate only on form submission
  119. ## Next
  120. Learn about [AI SDK integration](/docs/ai-sdk).