diff --git a/.gitignore b/.gitignore index 86aaedee1..382feba65 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ yarn.lock /index.js validator.js validator.min.js +.idea diff --git a/README.md b/README.md index 4974b6104..f7a9cf653 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ Validator | Description **isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).

`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, ignore_max_length: false }`.

`require_tld` - If set to false the validator will not check if the domain includes a TLD.
`allow_underscores` - if set to true, the validator will allow underscores in the domain.
`allow_trailing_dot` - if set to true, the validator will allow the domain to end with a `.` character.
`allow_numeric_tld` - if set to true, the validator will allow the TLD of the domain to be made up solely of numbers.
`allow_wildcard` - if set to true, the validator will allow domains starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`).
`ignore_max_length` - if set to true, the validator will not check for the standard max length of a domain.
**isFreightContainerID(str)** | alias for `isISO6346`, check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **isFullWidth(str)** | check if the string contains any full-width chars. +**isValidGraphQLQuery(str [, options])** | check if the string is valid graphql query (note: uses parse function from graphql package).

`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid graphql query values. **isHalfWidth(str)** | check if the string contains any half-width chars. **isHash(str, algorithm)** | check if the string is a hash of type algorithm.

Algorithm is one of `['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']`. **isHexadecimal(str)** | check if the string is a hexadecimal number. diff --git a/src/index.js b/src/index.js index c47674595..082e550de 100644 --- a/src/index.js +++ b/src/index.js @@ -129,6 +129,7 @@ import isLicensePlate from './lib/isLicensePlate'; import isStrongPassword from './lib/isStrongPassword'; import isVAT from './lib/isVAT'; +import isValidGraphQLQuery from "./lib/isValidGraphQLQuery"; const version = '13.15.0'; @@ -245,6 +246,7 @@ const validator = { isLicensePlate, isVAT, ibanLocales, + isValidGraphQLQuery, }; export default validator; diff --git a/src/lib/isValidGraphQLQuery.js b/src/lib/isValidGraphQLQuery.js new file mode 100644 index 000000000..b81e45732 --- /dev/null +++ b/src/lib/isValidGraphQLQuery.js @@ -0,0 +1,23 @@ +import assertString from './util/assertString'; +import { parse } from 'graphql'; +import merge from './util/merge'; + +const default_json_options = { + allow_primitives: false, +}; + +export default function isValidGraphQLQuery(input, options) { + assertString(input); + try { + options = merge(options, default_json_options); + let primitives = []; + if (options.allow_primitives) { + primitives = [null, false, true]; + } + + const obj = parse(input); + return includes(primitives, obj) || (!!obj && typeof obj === 'object'); + } catch (e) { + return false; + }; +}