ok-computer icon indicating copy to clipboard operation
ok-computer copied to clipboard

New validators

Open richardscarrott opened this issue 2 years ago • 5 comments

const uppercase = create(
  value => typeof value === 'string' && value.toUpperCase() === value
)('Expected uppercase string');

const lowercase = create(
  value => typeof value === 'string' && value.toLowerCase() === value
)('Expected lowercase string');

richardscarrott avatar May 03 '22 14:05 richardscarrott

const minmax = (minValue: number, maxValue: number = minValue) => err(and(min(minValue), max(maxValue)), `Expected number between ${minValue} and ${maxValue}`);

richardscarrott avatar Sep 04 '22 18:09 richardscarrott

const truthy = create(value => !!value)('Expected truthy value');
const falsy = create(value => !value)('Expected falsy value');

richardscarrott avatar Sep 04 '22 18:09 richardscarrott

// `like` or `objectLike` or `looseObject` or `objectWith` or `objectContaining`
const like = (validators) => object(validators, { allowUnknown: true });

richardscarrott avatar Sep 04 '22 19:09 richardscarrott

const trimmed = create(
  (value) => typeof value === "string" && value === value.trim()
)("Expected trimmed string");

richardscarrott avatar Sep 13 '22 19:09 richardscarrott

const any = create(() => true)('Expected any');

Could be useful when using array and object, e.g.

// Don't really care what's in the `errors` array or `headers` property, but don't want to loosen the response validator `allowUnknown`.
const gqlResponse = object({
   data: object({ id: string }),
   errors: or(undef, array(any)),
   headers: any
});

richardscarrott avatar Sep 16 '22 15:09 richardscarrott