$validate<T extends DOMNode> function

T? $validate<T extends DOMNode>({
  1. bool preValidate()?,
  2. DOMNodeValidator<T>? validate,
  3. T? node,
  4. DOMNodeInstantiator<T>? instantiator,
  5. bool rethrowErrors = false,
})

Validates a DOMNode before return it.

  • node: the DOMNode instance.
  • instantiator: the node instantiator, in case of node is null.
  • preValidate: validates the node before the instance is defined/created.
  • validate: validates a node after the instance is defined/created

Implementation

T? $validate<T extends DOMNode>(
    {bool Function()? preValidate,
    DOMNodeValidator<T>? validate,
    T? node,
    DOMNodeInstantiator<T>? instantiator,
    bool rethrowErrors = false}) {
  if (preValidate != null) {
    try {
      var preValid = preValidate();
      if (!preValid) return null;
    } catch (e, s) {
      if (rethrowErrors) {
        rethrow;
      } else {
        domBuilderLog("Error calling 'preValidate' function: $preValidate",
            error: e, stackTrace: s);
      }
    }
  }

  var theNode = node;
  if (theNode == null && instantiator != null) {
    try {
      theNode = instantiator();
    } catch (e, s) {
      domBuilderLog("Error calling 'instantiator' function: $instantiator",
          error: e, stackTrace: s);
    }
  }

  if (theNode == null) return null;

  if (validate != null) {
    try {
      var valid = validate(theNode);
      if (!valid) return null;
    } catch (e, s) {
      domBuilderLog("Error calling 'validate' function: $validate",
          error: e, stackTrace: s);
    }
  }

  return theNode;
}