Skip to content

l.oneOf()

Passes when the value equals one of values, compared with ==. The output is the matching allowed value, so l.oneOf([1, 2]) turns an input of 1.0 into 1. Returns an OneOfValidator<T, T?>. Use it for enums serialized by name or by @JsonValue.

OneOfValidator<T, T?> oneOf<T extends Object>(List<T> values, {String? message, MessageBuilder? messageBuilder})
Parameter Type Meaning
values List<T> The allowed values. Must not be empty; an empty list throws ArgumentError when the validator is built.
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 notOneOf. Default message: value must be one of: admin, member, listing the allowed values, with params['allowed']. value is replaced by the field name when there is one.

An enum parameter maps to l.oneOf([...]) with the enum’s serialized values: the names, or @JsonValue, @JsonEnum(fieldRename:, valueField:), @MappableEnum and @MappableValue when present.

final role = l.oneOf(['admin', 'member']);
role.validate(input)+2-2
  1. valid: 'admin'
  2. invalid: 'owner'value must be one of: admin, member
  3. invalid: 'Admin'value must be one of: admin, member
  4. valid: null

The output is the allowed value, not the input:

ValidationSuccess(data: 1)
ValidationSuccess(data: admin)