|
| 1 | +import { getCustomStaticPath } from '@/utils/getCustomStaticPath'; |
| 2 | + |
| 3 | +export const meta = { |
| 4 | + title: 'Field-level validation', |
| 5 | + description: 'Learn how to enable field level validation in your model schema', |
| 6 | + platforms: [ |
| 7 | + 'android', |
| 8 | + 'angular', |
| 9 | + 'flutter', |
| 10 | + 'javascript', |
| 11 | + 'nextjs', |
| 12 | + 'react', |
| 13 | + 'react-native', |
| 14 | + 'swift', |
| 15 | + 'vue' |
| 16 | + ] |
| 17 | +}; |
| 18 | + |
| 19 | +export function getStaticPaths() { |
| 20 | + return getCustomStaticPath(meta.platforms); |
| 21 | +} |
| 22 | + |
| 23 | +export function getStaticProps(context) { |
| 24 | + return { |
| 25 | + props: { |
| 26 | + platform: context.params.platform, |
| 27 | + meta |
| 28 | + } |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +You can enable field-level validation in your model schema by chaining a `validate` function to the field. |
| 33 | + |
| 34 | +## Examples |
| 35 | + |
| 36 | +```ts title="amplify/data/resource.ts" |
| 37 | +const schema = a.schema({ |
| 38 | + Todo: a.model({ |
| 39 | + content: a.string().validate(v => |
| 40 | + v |
| 41 | + .minLength(1, 'Content must be at least 1 character long') |
| 42 | + .maxLength(100, 'Content must be less than 100 characters') |
| 43 | + .matches('^[a-zA-Z0-9\\\\s]+$', 'Content must contain only letters, numbers, and spaces') |
| 44 | + ) |
| 45 | + }) |
| 46 | + .authorization(allow => [allow.publicApiKey()]) |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +## Supported validators |
| 51 | + |
| 52 | +### String Validators |
| 53 | +For `string` fields: |
| 54 | + |
| 55 | +| Validator | Description | Parameters | Example | |
| 56 | +| --- | --- | --- | --- | |
| 57 | +| `minLength` | Validates that a string field has at least the specified length | • `length`: The minimum length required<br/>• `errorMessage`: Optional custom error message | `a.string().validate(v => v.minLength(5, 'String must be at least 5 characters'))` | |
| 58 | +| `maxLength` | Validates that a string field does not exceed the specified length | • `length`: The maximum length allowed<br/>• `errorMessage`: Optional custom error message | `a.string().validate(v => v.maxLength(100, 'String must be at most 100 characters'))` | |
| 59 | +| `startsWith` | Validates that a string field starts with the specified prefix | • `prefix`: The prefix the string must start with<br/>• `errorMessage`: Optional custom error message | `a.string().validate(v => v.startsWith("prefix-", 'String must start with prefix-'))` | |
| 60 | +| `endsWith` | Validates that a string field ends with the specified suffix | • `suffix`: The suffix the string must end with<br/>• `errorMessage`: Optional custom error message | `a.string().validate(v => v.endsWith("-suffix", 'String must end with -suffix'))` | |
| 61 | +| `matches` | Validates that a string field matches the specified regex pattern using the **Java regex engine**. See notes below. | • `pattern`: The regex pattern the string must match<br/>• `errorMessage`: Optional custom error message | `a.string().validate(v => v.matches("^[a-zA-Z0-9]+$", 'String must match the pattern'))` | |
| 62 | + |
| 63 | +<Callout> |
| 64 | + |
| 65 | +**Note:** Our schema transformer uses the Java regex engine under the hood. Because of how TypeScript processes string literals, you must quadruple-escape special regex characters in your schema. In a TypeScript string literal, writing `\\\\s` produces the string `\\s`, which is the correct form for the Java regex engine. If you write `\\s`, it produces `\s`, which is invalid. Therefore, for the `matches` validator, ensure you use quadruple-escaping. For example: |
| 66 | +`a.string().validate(v => v.matches("^[a-zA-Z0-9\\\\s]+$", 'Content must contain only letters, numbers, and spaces'))` |
| 67 | + |
| 68 | +</Callout> |
| 69 | + |
| 70 | +### Numeric Validators |
| 71 | +For `integer` and `float` fields: |
| 72 | + |
| 73 | +| Validator | Description | Parameters | Example | |
| 74 | +| --- | --- | --- | --- | |
| 75 | +| `gt` | Validates that a numeric field is greater than the specified value | • `value`: The value the field must be greater than<br/>• `errorMessage`: Optional custom error message | `a.integer().validate(v => v.gt(10, 'Must be greater than 10'))` | |
| 76 | +| `gte` | Validates that a numeric field is greater than or equal to the specified value | • `value`: The value the field must be greater than or equal to<br/>• `errorMessage`: Optional custom error message | `a.integer().validate(v => v.gte(10, 'Must be at least 10'))` | |
| 77 | +| `lt` | Validates that a numeric field is less than the specified value | • `value`: The value the field must be less than<br/>• `errorMessage`: Optional custom error message | `a.integer().validate(v => v.lt(10, 'Must be less than 10'))` | |
| 78 | +| `lte` | Validates that a numeric field is less than or equal to the specified value | • `value`: The value the field must be less than or equal to<br/>• `errorMessage`: Optional custom error message | `a.integer().validate(v => v.lte(10, 'Must be at most 10'))` | |
| 79 | +| `positive` | Validates that a numeric field is positive | • `errorMessage`: Optional custom error message | `a.integer().validate(v => v.positive('Must be positive'))` | |
| 80 | +| `negative` | Validates that a numeric field is negative | • `errorMessage`: Optional custom error message | `a.integer().validate(v => v.negative('Must be negative'))` | |
| 81 | + |
| 82 | +<Callout> |
| 83 | + |
| 84 | +**Note:** Currently, we only support validation on **non-array** fields of type `string`, `integer`, and `float`. |
| 85 | + |
| 86 | +</Callout> |
0 commit comments