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

Scalar type factory (range validation, custom conditions) #757

Open
andreialecu opened this issue Mar 12, 2021 · 1 comment
Open

Scalar type factory (range validation, custom conditions) #757

andreialecu opened this issue Mar 12, 2021 · 1 comment

Comments

@andreialecu
Copy link

andreialecu commented Mar 12, 2021

Is your feature request related to a problem? Please describe.

I'd like to define some scalars that are for example positive integers limited to values between 0 to 6 (for a weekday index).

Describe the solution you'd like

It seems it would be possible to expose a factory method that could return a custom scalar with custom validation.

For example:

import { PositiveIntResolver, CustomStringResolver } from 'graphql-scalars';

const myResolverMap = {
  Weekday: PositiveIntResolver.create({name: "Weekday", description: "A value between 0 (Monday) and 6 (Sunday)", min: 0, max: 6}),
  CustomId: CustomStringResolver.create({name: "CustomId", description: "A string with custom validation", validation: /some-regex/ })
  ...

Describe alternatives you've considered

Manually defining my own scalars.

Additional context

Edit: it appears this is already a thing for regex validation: https://github.com/Urigo/graphql-scalars#using-the-regularexpression-scalar

Would really like it for numeric values as well.

@Betree
Copy link

Betree commented May 31, 2022

This could be useful for us as well to implement things like a Percentage scalar that accepts float numbers between 0 and 100. Not too difficult to implement from scratch, but we would have used graphql-scalars if there was a simple helper for this like the one mentioned above:

import { PositiveFloatResolver } from 'graphql-scalars';

export const Percentage = PositiveFloatResolver.create({ name: 'Percentage', min: 0, max: 100 })

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

No branches or pull requests

3 participants