Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Mar 17, 2025

Field-level validation

You can enable field-level validation in your model schema by chaining a validate function to the field.

Examples

amplify/data/resource.ts
const schema = a.schema({
Todo: a.model({
content: a.string().validate(v =>
v
.minLength(1, 'Content must be at least 1 character long')
.maxLength(100, 'Content must be less than 100 characters')
.matches('^[a-zA-Z0-9\\\\s]+$', 'Content must contain only letters, numbers, and spaces')
)
})
.authorization(allow => [allow.publicApiKey()])
});

Supported validators

String Validators

For string fields:

ValidatorDescriptionParametersExample
minLengthValidates that a string field has at least the specified lengthlength: The minimum length required
errorMessage: Optional custom error message
a.string().validate(v => v.minLength(5, 'String must be at least 5 characters'))
maxLengthValidates that a string field does not exceed the specified lengthlength: The maximum length allowed
errorMessage: Optional custom error message
a.string().validate(v => v.maxLength(100, 'String must be at most 100 characters'))
startsWithValidates that a string field starts with the specified prefixprefix: The prefix the string must start with
errorMessage: Optional custom error message
a.string().validate(v => v.startsWith("prefix-", 'String must start with prefix-'))
endsWithValidates that a string field ends with the specified suffixsuffix: The suffix the string must end with
errorMessage: Optional custom error message
a.string().validate(v => v.endsWith("-suffix", 'String must end with -suffix'))
matchesValidates 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
errorMessage: Optional custom error message
a.string().validate(v => v.matches("^[a-zA-Z0-9]+$", 'String must match the pattern'))

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: a.string().validate(v => v.matches("^[a-zA-Z0-9\\\\s]+$", 'Content must contain only letters, numbers, and spaces'))

Numeric Validators

For integer and float fields:

ValidatorDescriptionParametersExample
gtValidates that a numeric field is greater than the specified valuevalue: The value the field must be greater than
errorMessage: Optional custom error message
a.integer().validate(v => v.gt(10, 'Must be greater than 10'))
gteValidates that a numeric field is greater than or equal to the specified valuevalue: The value the field must be greater than or equal to
errorMessage: Optional custom error message
a.integer().validate(v => v.gte(10, 'Must be at least 10'))
ltValidates that a numeric field is less than the specified valuevalue: The value the field must be less than
errorMessage: Optional custom error message
a.integer().validate(v => v.lt(10, 'Must be less than 10'))
lteValidates that a numeric field is less than or equal to the specified valuevalue: The value the field must be less than or equal to
errorMessage: Optional custom error message
a.integer().validate(v => v.lte(10, 'Must be at most 10'))
positiveValidates that a numeric field is positiveerrorMessage: Optional custom error messagea.integer().validate(v => v.positive('Must be positive'))
negativeValidates that a numeric field is negativeerrorMessage: Optional custom error messagea.integer().validate(v => v.negative('Must be negative'))

Note: Currently, we only support validation on non-array fields of type string, integer, and float.