Download the PHP package shmax/graphql-php-validation-toolkit without Composer

On this page you can find all versions of the php package shmax/graphql-php-validation-toolkit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package graphql-php-validation-toolkit

graphql-php-validation-toolkit

License PHPStan lvl-6 Coverage Status Latest Stable Version

GraphQL is great when it comes to validating types and checking syntax, but isn't much help when it comes to providing additional validation on user input. The authors of GraphQL have generally opined that the correct response to bad user input is not to throw an exception, but rather to return any validation feedback along with the result.

As Lee Byron explains here:

...allow for data for a user-facing report in the payload of your mutation. It's often the case that mutation payloads include a "didSucceed" field and a "userError" field. If your UI requires rich information about potential errors, then you should include this information in your payload as well.

That's where this small library comes in.

graphql-php-validation-toolkit extends the built-in definitions provided by the wonderful graphql-php library with a new ValidatedFieldDefinition class. Simply instantiate one of these in place of the usual field config, add validate callback properties to your args definitions, and the type of your field will be replaced by a new, dynamically-generated ResultType with queryable error fields for each of your args. It's a recursive process, so your args can have InputObjectType types with subfields and validate callbacks of their own. Your originally-defined type gets moved to the result field of the generated type.

Installation

Via composer:

Documentation

Basic Usage

In a nutshell, replace your usual vanilla field definition with an instance of ValidatedFieldDefinition, and add validate callbacks to one or more of the args configs. Let's say you want to make a mutation called updateBook:

In the sample above, the book type property of your field definition will be replaced by a new dynamically-generated type called UpdateBookResultType.

The type generation process is recursive, traveling down through any nested InputObjectType or ListOf types and checking their fields for more validate callbacks. Every field definition--including the very top one--that has a validate callback will be represented by a custom, generated type with the following queryable fields:

Field Type Description
code int | <field-name>ErrorCode This will resolve to 0 for a valid field, otherwise 1. If errorCodes were provided, then this will be a custom generated Enum type.
msg string A plain, natural language description of the error.
suberrors <field-name>_Suberrors A suberrors field will be added to a generated field of type InputObjectType if any of the following are true:
  1. It is the root node
  2. The field has a validate method
  3. The InputObjectType is wrapped in a ListOfType

The top-level <field-name>ResultType will have a few additional fields:

Field Type Description
valid bool Resolves to true if all args and nested fields pass validation, false if not.
result mixed This is the original type you provided when declaring your field. Eg, If you specified type to be a Book, then the type of result will be Book.

You can then simply query for these fields along with result:

The Validate Callback

Any field definition can have a validate callback. The first argument passed to the validate callback will be the value to validate. If the value is valid, return 0, otherwise 1.

If you want to return an error message, return an array with the message in the second bucket:

Generated ListOf error types also have a path field that you can query so you can know the exact address in the multidimensional array of each item that failed validation:

Custom Error Codes

If you would like to use custom error codes, add an errorCodes property at the same level as your validate callback and feed it the path to a PHP native enum:

Keep in mind that the library will generate unique names for the error code types, and they can become quite long depending on how deeply they are nested in the field structure:

If this becomes a problem for you, be sure to provide a type setter (see example) that returns the type that was set, and then the generated name will simply be the name of the enum class that was passed in, plus "ErrorCode":

Managing Created Types

This library will create new types as needed. If you are using some kind of type manager to store and retrieve types, you can integrate it by providing a typeSetter callback. Make sure it returns the type that was set:

Examples

The best way to understand how all this works is to experiment with it. There are a series of increasingly complex one-page samples in the /examples folder. Each is accompanied by its own README.md, with instructions for running the code. Run each sample, and be sure to inspect the dynamically-generated types in ChromeiQL.

  1. basic-scalar-validation
  2. custom-error-types
  3. input-object-validation
  4. list-of-validation

Contribute

Contributions are welcome. Please refer to CONTRIBUTING.md for guidelines.


All versions of graphql-php-validation-toolkit with dependencies

PHP Build Version
Package Version
Requires php Version ^8
ext-json Version *
ext-mbstring Version *
ext-intl Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package shmax/graphql-php-validation-toolkit contains the following files

Loading the files please wait ....