validate function

Validator validate(
  1. bool condition(
    1. ByteSerializable
    ),
  2. Error error(
    1. ByteSerializable
    )
)

Creates a validator function

condition is a function that takes a ByteSerializable and returns the boolean result of the validation condition error is a function taking a ByteSerializable that generates an error to be thrown in the event that the condition fails and the caller wanted the validator to throw an exception with a detailed error message. This allows the error message to contain variables derived from the value being validated.

Implementation

Validator validate(bool Function(ByteSerializable) condition,
    Error Function(ByteSerializable) error) {
  return (ByteSerializable data, {bool shouldThrow = false}) {
    if (condition(data)) return true;
    return (shouldThrow) ? throw error(data) : false;
  };
}