ObjectValidator class
A validator for validating object structures (Map<String, dynamic>). Supports defining a schema, optional fields, extra key handling, and custom validation rules.
Constructors
- ObjectValidator({String invalidTypeMessage = "Must be a valid object"})
- Creates an instance of ObjectValidator.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- invalidTypeMessage → String
-
finalinherited
-
rules
→ List<
String? Function(Map< String, dynamic> ?)> -
Returns an immutable list of validation rules.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addRule(
String? rule(Map< String, dynamic> ?)) → Validator<Map< String, dynamic> > -
Adds a validation rule to the validator.
inherited
-
allowExtraKeys(
) → ObjectValidator - Allows the object to have extra keys beyond the defined schema.
-
build(
{bool returnAllErrors = false}) → String? Function(String?) -
Builds a function that validates a
String?input.inherited -
customValidation(
String? validator(Map< String, dynamic> )) → ObjectValidator - Adds a custom validation rule that checks the entire object.
-
deepEqual(
Map< String, dynamic> other, {String message = "Objects do not match"}) → ObjectValidator -
Ensures the object matches the given
otherobject exactly. -
exactKeys(
List< String> keys, {String message = "Unexpected keys found"}) → ObjectValidator -
Ensures that only the specified
keysare allowed in the object. Returns an error message if extra keys are present. -
nonEmpty(
{String message = "Object cannot be empty"}) → ObjectValidator - Ensures the object is not empty.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
optionalFields(
List< String> fields) → ObjectValidator - Specifies which fields are optional in the object.
-
parseValue(
String? input) → Map< String, dynamic> ? -
Parses a JSON string into a Map<String, dynamic>.
Returns
nullif parsing fails. -
shape(
Map< String, Validator> schema) → ObjectValidator - Defines the structure of the expected object.
-
toString(
) → String -
A string representation of this object.
inherited
-
validate(
dynamic value, {bool returnAllErrors = false, bool returnAsList = false}) → Object? -
Validates the given
valueagainst the defined schema and rules. -
validateStrict(
Map< String, dynamic> ? value, {bool returnAllErrors = false, bool returnAsList = false}) → Object? - Validates the object strictly, including type checks for each field.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited