-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.ts
68 lines (59 loc) · 1.61 KB
/
validate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2022-latest the graphqland authors. All rights reserved. MIT license.
// This module is browser compatible.
import {
GraphQLRequestOptions,
GraphQLRequestParams,
hasOwn,
isNativeObject,
isNull,
isObject,
isString,
json,
Result,
} from "./deps.ts";
export function validateRequestParams(
value: json,
): Result<GraphQLRequestOptions & GraphQLRequestParams, TypeError> {
if (!isObject(value) || !isNativeObject(value)) {
return Result.err(TypeError(`Value must be JSON object.`));
}
if (!hasOwn("query", value)) {
return Result.err(TypeError(`Missing field. "query"`));
}
if (!isString(value.query)) {
return Result.err(TypeError(`"query" must be string.`));
}
if (hasOwn("variables", value)) {
if (
(!isNull(value.variables) &&
!(isObject(value.variables) && isNativeObject(value.variables)))
) {
return Result.err(TypeError(`"variables" must be object or null.`));
}
}
if (
hasOwn("operationName", value) &&
!(isNull(value.operationName) || isString(value.operationName))
) {
return Result.err(TypeError(`"operationName" must be string or null.`));
}
if (hasOwn("extensions", value)) {
if (
!(isNull(value.extensions) ||
(isObject(value.extensions) && isNativeObject(value.extensions)))
) {
return Result.err(
TypeError(`"extensions" must be object or null.`),
);
}
}
const { query, variables, operationName, extensions } = value as
& GraphQLRequestParams
& GraphQLRequestOptions;
return Result.ok({
operationName,
variables,
extensions,
query,
});
}