graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Proposal: new validation directives

Open Andy2003 opened this issue 3 years ago • 7 comments

A user in slack asked for an ability to enforce the maximum number of returned entities.

To achieve this, I would propose the following new directives:

directive @minValue(value:Float!) on ARGUMENT_DEFINITION | FIELD_DEFINITION | INPUT_FIELD_DEFINITION
directive @maxValue(value:Float!) on ARGUMENT_DEFINITION | FIELD_DEFINITION | INPUT_FIELD_DEFINITION

so it can be used like this:

type User{
  name: String
  size: Float @minValue(value: 20) @maxValue(value: 250)
}

input UserOptions {
  limit: Int @minValue(value: 0) @maxValue(value: 100)
  skip: Int @minValue(value: 0)
}

Andy2003 avatar Mar 10 '21 09:03 Andy2003

To enforce a maximum number of entities you probably want to use the existing limit argument;

query {
    movies {
        genres(limit: 2) {
            name
        }
    }
}

That being said - Validation directives are something we are looking into, and we are thinking of supporting a similar feature set to https://www.npmjs.com/package/graphql-constraint-directive.

danstarns avatar Mar 10 '21 10:03 danstarns

To enforce a maximum number of entities you probably want to use the existing limit argument;

this is not enforcing a maximum, since the user can query:

query {
    movies {
        genres(limit: 20000000) {
            name
        }
    }
}

But with the proposed directives implemented this query would will fail with an error.

Validation directives are something we are looking into, and we are thinking of supporting a similar feature set to https://www.npmjs.com/package/graphql-constraint-directive.

Sounds great!

Andy2003 avatar Mar 10 '21 10:03 Andy2003

@Andy2003 Ah yes I see the value in the max limit here 👍

danstarns avatar Mar 10 '21 13:03 danstarns

I am trying to get https://www.npmjs.com/package/graphql-constraint-directive to work with this package. Is this possible?

TreeMan360 avatar Apr 21 '21 09:04 TreeMan360

@TreeMan360 Hey! We don't directly support this. Due to the way the library works - Given some Type Definitons we produce a CURD schema with autogenerated inputs. As the inputs are auto-generated any directive you apply on the initial type is not preserved. You can however extend upon our autogenerated inputs to get this to work, you need to play around with some tools and we can sure look at making this easier but here is the gist showing how to do it: https://gist.github.com/danstarns/e3956e1633ab9866bd0172dc85470659

danstarns avatar Apr 21 '21 10:04 danstarns

This is something we'll look at (at some point), and just pointing out a couple of proposed solutions from some other issues which I will be closing as duplicates of this:

  • #1550
  • #1383

darrellwarde avatar Aug 25 '22 16:08 darrellwarde

For clarity, dropping a comment that I'd love Zod support in here somehow (should probably support every big validation lib tbh).

corysimmons avatar Aug 25 '22 17:08 corysimmons