Skip to content

Commit f57018f

Browse files
author
apsin40
committed
feat(isGraphQlValidator): add validation for graphQl queries
1 parent d91af4b commit f57018f

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ yarn.lock
1010
/index.js
1111
validator.js
1212
validator.min.js
13+
.idea
1314

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Validator | Description
113113
**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).<br/><br/>`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 }`.<br/><br/>`require_tld` - If set to false the validator will not check if the domain includes a TLD.<br/>`allow_underscores` - if set to true, the validator will allow underscores in the domain.<br/>`allow_trailing_dot` - if set to true, the validator will allow the domain to end with a `.` character.<br/>`allow_numeric_tld` - if set to true, the validator will allow the TLD of the domain to be made up solely of numbers.<br />`allow_wildcard` - if set to true, the validator will allow domains starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`).<br/>`ignore_max_length` - if set to true, the validator will not check for the standard max length of a domain.<br/>
114114
**isFreightContainerID(str)** | alias for `isISO6346`, check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification.
115115
**isFullWidth(str)** | check if the string contains any full-width chars.
116+
**isValidGraphQLQuery(str [, options])** | check if the string is valid graphql query (note: uses parse function from graphql package).<br/><br/>`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.
116117
**isHalfWidth(str)** | check if the string contains any half-width chars.
117118
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']`.
118119
**isHexadecimal(str)** | check if the string is a hexadecimal number.

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ import isLicensePlate from './lib/isLicensePlate';
129129
import isStrongPassword from './lib/isStrongPassword';
130130

131131
import isVAT from './lib/isVAT';
132+
import isValidGraphQLQuery from "./lib/isValidGraphQLQuery";
132133

133134
const version = '13.15.0';
134135

@@ -245,6 +246,7 @@ const validator = {
245246
isLicensePlate,
246247
isVAT,
247248
ibanLocales,
249+
isValidGraphQLQuery,
248250
};
249251

250252
export default validator;

src/lib/isValidGraphQLQuery.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import assertString from './util/assertString';
2+
import { parse } from 'graphql';
3+
import merge from './util/merge';
4+
5+
const default_json_options = {
6+
allow_primitives: false,
7+
};
8+
9+
export default function isValidGraphQLQuery(input, options) {
10+
assertString(input);
11+
try {
12+
options = merge(options, default_json_options);
13+
let primitives = [];
14+
if (options.allow_primitives) {
15+
primitives = [null, false, true];
16+
}
17+
18+
const obj = parse(input);
19+
return includes(primitives, obj) || (!!obj && typeof obj === 'object');
20+
} catch (e) {
21+
return false;
22+
};
23+
}

0 commit comments

Comments
 (0)