const UndefinedFromNull = <T extends S.Schema.Any>(schema: T) =>
S.transform(S.NullishOr(schema), S.NullOr(schema), {
strict: true,
encode: v => v || null,
decode: v => v || null
});
export const UnDeleteableRootField = <T extends S.Schema.Any>(schema: T) =>
S.optionalWith(UndefinedFromNull(schema), { nullable: true, exact: true });
/* test for extra clarity */
describe('UnDeleteableRootField', () => {
const schema = S.Struct({ foo: UnDeleteableRootField(S.String) });
const decodeSync = S.decodeSync(schema);
it('should convert undefined/null/missing to missing', () => {
const whenMissing = decodeSync({});
const whenUndefined = decodeSync({ foo: undefined });
const whenNull = decodeSync({ foo: null });
expect(whenMissing).not.toHaveProperty('foo');
expect(whenUndefined).not.toHaveProperty('foo');
expect(whenNull).not.toHaveProperty('foo');
});
});
const UndefinedFromNull = <T extends S.Schema.Any>(schema: T) =>
S.transform(S.NullishOr(schema), S.NullOr(schema), {
strict: true,
encode: v => v || null,
decode: v => v || null
});
export const UnDeleteableRootField = <T extends S.Schema.Any>(schema: T) =>
S.optionalWith(UndefinedFromNull(schema), { nullable: true, exact: true });
/* test for extra clarity */
describe('UnDeleteableRootField', () => {
const schema = S.Struct({ foo: UnDeleteableRootField(S.String) });
const decodeSync = S.decodeSync(schema);
it('should convert undefined/null/missing to missing', () => {
const whenMissing = decodeSync({});
const whenUndefined = decodeSync({ foo: undefined });
const whenNull = decodeSync({ foo: null });
expect(whenMissing).not.toHaveProperty('foo');
expect(whenUndefined).not.toHaveProperty('foo');
expect(whenNull).not.toHaveProperty('foo');
});
});