Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New validators #3

Open
richardscarrott opened this issue May 3, 2022 · 5 comments
Open

New validators #3

richardscarrott opened this issue May 3, 2022 · 5 comments

Comments

@richardscarrott
Copy link
Owner

richardscarrott commented May 3, 2022

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 richardscarrott changed the title Add uppercase & lowercase New validators Sep 4, 2022
@richardscarrott
Copy link
Owner Author

richardscarrott commented Sep 4, 2022

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

@richardscarrott
Copy link
Owner Author

richardscarrott commented Sep 4, 2022

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

@richardscarrott
Copy link
Owner Author

richardscarrott commented Sep 4, 2022

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

@richardscarrott
Copy link
Owner Author

richardscarrott commented Sep 13, 2022

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

@richardscarrott
Copy link
Owner Author

richardscarrott commented Sep 16, 2022

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
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant