Skip to content

.required()

Adds one validation: the value is not null. Inside a schema, a missing key counts as null, so .required() is also what makes a field mandatory. It says nothing about emptiness: '' and [] pass. The return type drops the ?, so l.string().required() is a StringValidator<String> and success data is a String. It chains in any position. Optional by default explains the design.

StringValidator<String> required({String? message, MessageBuilder? messageBuilder})
// and the same on every other validator type, each returning its non-nullable form
Parameter Type Meaning
message String? Replaces the default error message.
messageBuilder MessageBuilder? Builds the message from the issue. message wins when both are given. See Message builders.

Code required. Default message: value is required. value is replaced by the field name when there is one.

The generator adds .required() to every parameter that is non-nullable, not dynamic, and has no default (a constructor default, @Default, or @JsonKey(defaultValue:)). Dart’s required keyword plays no part.

final name = l.string().required();
name.validate(input)+2-1
  1. valid: 'Ada'
  2. valid: ''
  3. invalid: nullvalue is required