Suggestion to Add a `partial` Option for the `strict` Field in Transforms

I was wondering if it would not be a good idea to add other options for the strict field in transforms than
true
and false, like partial.

I hacked this transformPartial helper to transform form entries to an object and back. Its only purpose is to let me benefit from type checking while letting the target schema handle the check for required fields.

Using Schema.transform directly was forcing me to type cast the decoder's return type or to use strict: false, because the decoder return type is inferred as { foo: Foo }, since other fields are treated in a generic way and are inferred as a record, so TS was complaining about missing required fields.

transformPartial(
    S.Array(S.Tuple(S.String, (S.Union(S.String, S.instanceOf(File))))),
    S.Struct({ foo: FooSchema, ...otherFields }),
    {
        encode: encoder,
        decode: decoder
    }
);

const transformPartial = <FA, TA, RA, FB, TB, RB>(
    from: S.Schema<TA, FA, RA>,
    to: S.Schema<TB, FB, RB>,
    options: {
        decode: (encoded: TA) => Partial<FB>,
        encode: (encoded: FB) => TA,
    }
) => S.transform(
    from,
    to,
    {
        decode: options.decode as (a: TA) => FB,
        encode: options.encode
    }
);
Was this page helpful?