arktypea
arktype13mo ago
Tony

Optional required pairs?

I have a type that has all optional properties. However I want to want to make 2 options a pair - if one exists, the other is required.

An example:
const props = type({
    'propA?': 'string',
    'propB?': 'string',
    'propC?': 'string'
});

// valid
props({ propA: 'test' });

// not valid - propB requires propC as well, and vice versa
props({ propB: 'test' });
props({ propC: 'test' });

// valid - both provided
props({ propB: 'test', propC: 'test' });


Is this doable? I'm using Arktype for validating incoming JSON in a server, so the use case for it is changing passwords, if you want to change your password you need to provide the current password as well, and maybe the new password a second time to confirm it
Was this page helpful?