performTypeValidation method

void performTypeValidation(
  1. String? path,
  2. dynamic type,
  3. dynamic value,
  4. List<ValidationResult> results,
)

Validates a given value to match specified type. The type can be defined as a Schema, type, a type name or TypeCode When type is a Schema, it executes validation recursively against that Schema.

  • path a dot notation path to the value.
  • type a type to match the value type
  • value a value to be validated.
  • results a list with validation results to add new results.

See performValidation

Implementation

void performTypeValidation(String? path, dynamic type, dynamic value,
    List<ValidationResult> results) {
  // If type it not defined then skip
  if (type == null) return;

  // Perform validation against the schema
  if (type is Schema) {
    var schema = type;
    schema.performValidation(path, value, results);
    return;
  }

  // If value is null then skip
  value = ObjectReader.getValue(value);
  if (value == null) return;

  var name = path ?? 'value';
  var valueType = TypeConverter.toTypeCode(value);

  // Match types
  if (TypeMatcher.matchType(type, valueType, value)) return;

  results.add(ValidationResult(
      path,
      ValidationResultType.Error,
      'TYPE_MISMATCH',
      name +
          ' type must be ' +
          _typeToString(type) +
          ' but found ' +
          _typeToString(valueType),
      type,
      valueType));
}