parse method

ZRes<T> parse(
  1. Object? val
)

Parses val using the configured transformation pipeline and returns a ZRes containing either the successfully parsed value or a validation error.

See ZRes for more details on success and error handling.

Throws a ZodArtInternalException only in case of an unexpected internal failure, such as a misconfigured transformation or an unhandled exception. This should never happen during normal usage, and typically indicates a bug in the validation schema or library internals.

Implementation

ZRes<T> parse(Object? val) {
  try {
    // Forced to use this workaround instead of flatMap not to expose fpdart
    final parseRes = _config.fns.fold(ZRes.success(val), (current, transformation) {
      if (current.isError) return current;
      final configuredTransformer = transformation.fn(_config);
      return configuredTransformer(current.value);
    });

    // NOTE: Need to fix the generic type for Left as it is untouched during processing above
    return _fixLeftGenericType(parseRes);
  } on ZodArtInternalException catch (e, stack) {
    /// NOTE: Add ZBaseConfig to the exception and throw
    throw ZodArtInternalException(
      e.message,
      value: val,
      config: _config,
      stackTrace: stack,
    );
  } catch (e, stack) {
    throw ZodArtInternalException(
      'Unknown exception: "$e"',
      value: val,
      config: _config,
      stackTrace: stack,
    );
  }
}