Ensure a property is greater than a different property?

Is there a way to set a constraint for a number property so that it must be larger than a different property, like so?
type({ min: 'number > 1', max: 'number > min' })
type({ min: 'number > 1', max: 'number > min' })
3 Replies
Apteryx
Apteryx4w ago
https://arktype.io/docs/expressions#narrow
const Thing = type({ min: 'number > 1', max: 'number' }).narrow((data, ctx) => {
if (data.max > data.min) return true;
return ctx.reject({
expected: `more than ${data.min}`,
actual: `${data.max}`,
path: ['max'],
});
});

const thing = Thing({ min: 2, max: 1 });
console.log(thing instanceof type.errors); // true
const Thing = type({ min: 'number > 1', max: 'number' }).narrow((data, ctx) => {
if (data.max > data.min) return true;
return ctx.reject({
expected: `more than ${data.min}`,
actual: `${data.max}`,
path: ['max'],
});
});

const thing = Thing({ min: 2, max: 1 });
console.log(thing instanceof type.errors); // true
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
bigboyjo
bigboyjoOP4w ago
I see. There's no way to do it at the type level via strings?
ssalbdivad
ssalbdivad4w ago
there is an open issue for this that I'd accept PRs on but may be a bit tricky: https://github.com/arktypeio/arktype/issues/485

Did you find this page helpful?