Validation

Validating Arguments

Lighthouse allows you to use Laravel's validation for your queries and mutations. The simplest way to leverage the built-in validation rules is to use the @rules directive.

type Mutation {
  createUser(
    name: String @rules(apply: ["required", "min:4"])
    email: String @rules(apply: ["email"])
  ): User
}

In the case of a validation error, Lighthouse will abort execution and return the validation messages as part of the response.

mutation {
  createUser(email: "hans@peter.xyz"){
    id
  }
}
{  
  "data":{  
    "foo":null
  },
  "errors":[  
    {  
      "message":"validation",
      "locations":[  
        {  
          "line":2,
          "column":13
        }
      ],
      "validation":[  
        "The name field is required."
      ]
    }
  ]
}

You can customize the error message for a particular argument.

@rules(apply: ["max:140"], messages: { max: "Tweets have a limit of 140 characters"})