confuser / graphql-constraint-directive

Validate GraphQL fields
ISC License
569 stars 74 forks source link

Expose validate functions #171

Open jcoc611-gvt opened 1 year ago

jcoc611-gvt commented 1 year ago

Hi there! Thanks for the awesome package!

Would you be willing to expose the validate functions in scalars/number.js and scalars/string.js? I would like to be able to manually validate fields on the client side without having to construct a GraphQL mutation.

I can probably make a PR if you are interested!

The scenario is being able to reuse these constraint directives to also validate things like HTML forms (which would not necessarily use GraphQL, but would logically still be the same types).

More context if you are interested: I'm writing a codegen plugin for @graphql-codegen/cli which takes in a declaration such as:

type User {
  email: String! @constraint(format: "email")
}

and produces:

import { validateString } from 'graphql-constraint-directive'; // this part missing
export const UserValidator = {
  email: (value: string) => validateString('email', {format: "email"}, value)
}
confuser commented 1 year ago

PR welcome