compile method

dynamic compile(
  1. ManagedType typeBeingValidated, {
  2. Type? relationshipInverseType,
})

Subclasses override this method to perform any one-time initialization tasks and check for correctness.

Use this method to ensure a validator is being applied to a property correctly. For example, a Validate.compare builds a list of expressions and ensures each expression's values are the same type as the property being validated.

The value returned from this method is available in ValidationContext.state when this instance's validate method is called.

typeBeingValidated is the type of the property being validated. If relationshipInverseType is not-null, it is a ManagedObject subclass and typeBeingValidated is the type of its primary key.

If compilation fails, throw a ValidateCompilationError with a message describing the issue. The entity and property will automatically be added to the error.

Implementation

dynamic compile(
  ManagedType typeBeingValidated, {
  Type? relationshipInverseType,
}) {
  switch (type) {
    case ValidateType.absent:
      return null;
    case ValidateType.present:
      return null;
    case ValidateType.oneOf:
      {
        return _oneOfCompiler(
          typeBeingValidated,
          relationshipInverseType: relationshipInverseType,
        );
      }
    case ValidateType.comparison:
      return _comparisonCompiler(
        typeBeingValidated,
        relationshipInverseType: relationshipInverseType,
      );
    case ValidateType.regex:
      return _regexCompiler(
        typeBeingValidated,
        relationshipInverseType: relationshipInverseType,
      );
    case ValidateType.length:
      return _lengthCompiler(
        typeBeingValidated,
        relationshipInverseType: relationshipInverseType,
      );
    default:
      return null;
  }
}