Skip to content

.strict()

Returns a copy of the schema that fails when the input has a key the schema does not declare. Each unknown key produces one unrecognizedKey issue at that key’s path, with params['key']. Only SchemaValidator has this method.

SchemaValidator<O> strict({String? message, MessageBuilder? messageBuilder})
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 unrecognizedKey. Default message: role is not an allowed key, where role is the unknown key.

The generator does not emit .strict(). Apply it to a generated schema where you use it: $UserSchema.strict().

final user = l.schema({'name': l.string().required()}).strict();
l.schema({'name': l.string().required()}).strict().validate(input)+1-1
  1. valid: {'name': 'Ada'}
  2. invalid: {'name': 'Ada', 'role': 'admin'}role: role is not an allowed key