zio-schema icon indicating copy to clipboard operation
zio-schema copied to clipboard

Should generators respect validations on schemas, and schema instances created from transformOrFail?

Open beem812 opened this issue 2 years ago • 1 comments

Current behavior:

  1. Create a schema with a validation, then derive a generator for that schema. running that generator produces values that do not meet the schemas validation.

  2. Create a schema via the .transformOrFail function, then derive a generator from that schema running the generator potentially produces no values, despite the condition for successful creation being something simple like strings longer than 5 characters

Potential solutions:

  1. filter the generator over the validation to produce valid instances (It is dangerous to allow someone to create a filter that may never return a value, so this option seems bad)
  2. Similar to one, for transformOrFail instances, keep creating until an instance is actually generated. Again dangerous if all cases fail.

beem812 avatar Nov 21 '22 17:11 beem812

I've been surprised by the current behaviour, actually. Don't know if it's possible but a solution might be to provide a generator for each validation and combine them. It looks like it's what refined-scalacheck does (and it's not just filters)

err0r500 avatar Feb 28 '23 17:02 err0r500