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.
Signature
Section titled “Signature”OneOfValidator<T, T?> oneOf<T extends Object>(List<T> values, {String? message, MessageBuilder? messageBuilder})Parameters
Section titled “Parameters”| 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.
Code generation
Section titled “Code generation”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.
Example
Section titled “Example”final role = l.oneOf(['admin', 'member']);enum Role { admin, member }
@Default(Role.member) Role role, // l.oneOf(["admin", "member"])required Role role, // l.oneOf(["admin", "member"]).required()role.validate(input)+2-2- valid:
'admin' - invalid:
'owner' - invalid:
'Admin' - valid:
null
The output is the allowed value, not the input:
ValidationSuccess(data: 1)ValidationSuccess(data: admin)