Executes all Validates for object
.
Validates the properties of object
according to its declared validators. Validators
are added to properties using Validate metadata. See Validate.
This method is invoked by ManagedObject.validate. Invoking this method directly will ignore any validations that occur by overriding ManagedObject.validate and should be avoided.
Pass an empty list for errors
to receive more details on any failed validations.
Source
static bool run( ManagedObject object, {ValidateOperation operation: ValidateOperation.insert, List<String> errors}) { errors ??= []; var valid = true; var validators = object.entity.validators; validators.forEach((validator) { if (!validator.definition.runOnInsert && operation == ValidateOperation.insert) { return; } if (!validator.definition.runOnUpdate && operation == ValidateOperation.update) { return; } if (validator.definition._builtinValidate == _BuiltinValidate.absent) { if (object.backingMap.containsKey(validator.attribute.name)) { valid = false; errors.add("Value for '${validator.attribute.name}' may not be included " "for ${_errorStringForOperation(operation)}s."); } } else if (validator.definition._builtinValidate == _BuiltinValidate.present) { if (!object.backingMap.containsKey(validator.attribute.name)) { valid = false; errors.add("Value for '${validator.attribute.name}' must be included " "for ${_errorStringForOperation(operation)}s."); } } else { var value = object.backingMap[validator.attribute.name]; if (value != null) { if (!validator._isValidFor(operation, validator.attribute, value, errors)) { valid = false; } } } }); return valid; }