nette / schema

📐 Validating data structures against a given Schema.
https://doc.nette.org/schema
Other
905 stars 26 forks source link

Support custom cast function #52

Closed JesusTheHun closed 1 year ago

JesusTheHun commented 1 year ago

It would like to be able to cast a value using a callable.

class Foo {
  public static function fromString($bar) {
    // some logic
  }
}

$schema = Expect::structure([
  'foo' => Expect::string()->castTo(fn ($v) => Foo::fromString($v));
  'foo2' => Expect::string()->castTo('Foo::fromString');
});

I'm willing to make a PR for this if you accept the feature to be integrated in the project.

JanTvrdik commented 1 year ago

The idea is good, but it would be better to use different method name, to more accurately describe the action, e.g. transform (that is the name used by Zod)