zapier-platform-schema 11.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/LICENSE +4 -0
  3. package/README.md +21 -0
  4. package/exported-schema.json +1418 -0
  5. package/lib/constants.js +21 -0
  6. package/lib/functional-constraints/deepNestedFields.js +68 -0
  7. package/lib/functional-constraints/index.js +35 -0
  8. package/lib/functional-constraints/labelWhenVisible.js +31 -0
  9. package/lib/functional-constraints/matchingKeys.js +35 -0
  10. package/lib/functional-constraints/mutuallyExclusiveFields.js +70 -0
  11. package/lib/functional-constraints/requiredSamples.js +56 -0
  12. package/lib/functional-constraints/searchOrCreateKeys.js +67 -0
  13. package/lib/functional-constraints/uniqueInputFieldKeys.js +67 -0
  14. package/lib/schemas/AppFlagsSchema.js +22 -0
  15. package/lib/schemas/AppSchema.js +148 -0
  16. package/lib/schemas/AuthenticationBasicConfigSchema.js +14 -0
  17. package/lib/schemas/AuthenticationCustomConfigSchema.js +14 -0
  18. package/lib/schemas/AuthenticationDigestConfigSchema.js +14 -0
  19. package/lib/schemas/AuthenticationOAuth1ConfigSchema.js +53 -0
  20. package/lib/schemas/AuthenticationOAuth2ConfigSchema.js +73 -0
  21. package/lib/schemas/AuthenticationSchema.js +118 -0
  22. package/lib/schemas/AuthenticationSessionConfigSchema.js +31 -0
  23. package/lib/schemas/BasicActionOperationSchema.js +59 -0
  24. package/lib/schemas/BasicCreateActionOperationSchema.js +26 -0
  25. package/lib/schemas/BasicDisplaySchema.js +92 -0
  26. package/lib/schemas/BasicHookOperationSchema.js +117 -0
  27. package/lib/schemas/BasicOperationSchema.js +73 -0
  28. package/lib/schemas/BasicPollingOperationSchema.js +41 -0
  29. package/lib/schemas/BulkReadSchema.js +72 -0
  30. package/lib/schemas/BulkReadsSchema.js +69 -0
  31. package/lib/schemas/BundleSchema.js +17 -0
  32. package/lib/schemas/CreateSchema.js +108 -0
  33. package/lib/schemas/CreatesSchema.js +78 -0
  34. package/lib/schemas/DynamicFieldsSchema.js +36 -0
  35. package/lib/schemas/FieldChoiceWithLabelSchema.js +37 -0
  36. package/lib/schemas/FieldChoicesSchema.js +40 -0
  37. package/lib/schemas/FieldOrFunctionSchema.js +40 -0
  38. package/lib/schemas/FieldSchema.js +183 -0
  39. package/lib/schemas/FieldsSchema.js +17 -0
  40. package/lib/schemas/FlatObjectSchema.js +45 -0
  41. package/lib/schemas/FunctionRequireSchema.js +28 -0
  42. package/lib/schemas/FunctionSchema.js +42 -0
  43. package/lib/schemas/FunctionSourceSchema.js +37 -0
  44. package/lib/schemas/HydratorsSchema.js +29 -0
  45. package/lib/schemas/KeySchema.js +30 -0
  46. package/lib/schemas/MiddlewaresSchema.js +35 -0
  47. package/lib/schemas/RedirectRequestSchema.js +41 -0
  48. package/lib/schemas/RefResourceSchema.js +24 -0
  49. package/lib/schemas/RequestSchema.js +109 -0
  50. package/lib/schemas/ResourceMethodCreateSchema.js +67 -0
  51. package/lib/schemas/ResourceMethodGetSchema.js +74 -0
  52. package/lib/schemas/ResourceMethodHookSchema.js +72 -0
  53. package/lib/schemas/ResourceMethodListSchema.js +76 -0
  54. package/lib/schemas/ResourceMethodSearchSchema.js +67 -0
  55. package/lib/schemas/ResourceSchema.js +168 -0
  56. package/lib/schemas/ResourcesMethodGetSchema.js +57 -0
  57. package/lib/schemas/ResourcesSchema.js +95 -0
  58. package/lib/schemas/ResultsSchema.js +25 -0
  59. package/lib/schemas/SearchOrCreateSchema.js +84 -0
  60. package/lib/schemas/SearchOrCreatesSchema.js +60 -0
  61. package/lib/schemas/SearchSchema.js +84 -0
  62. package/lib/schemas/SearchesSchema.js +54 -0
  63. package/lib/schemas/TriggerSchema.js +93 -0
  64. package/lib/schemas/TriggersSchema.js +61 -0
  65. package/lib/schemas/VersionSchema.js +18 -0
  66. package/lib/utils/buildDocs.js +213 -0
  67. package/lib/utils/exportSchema.js +23 -0
  68. package/lib/utils/links.js +27 -0
  69. package/lib/utils/makeSchema.js +27 -0
  70. package/lib/utils/makeValidator.js +150 -0
  71. package/package.json +40 -0
  72. package/schema.js +13 -0
@@ -0,0 +1,150 @@
1
+ 'use strict';
2
+
3
+ const jsonschema = require('jsonschema');
4
+ const links = require('./links');
5
+ const functionalConstraints = require('../functional-constraints');
6
+ const { flattenDeep, get } = require('lodash');
7
+
8
+ const ambiguousTypes = ['anyOf', 'oneOf', 'allOf'];
9
+
10
+ const makeLinks = (error, makerFunc) => {
11
+ if (typeof error.schema === 'string') {
12
+ return [makerFunc(error.schema)];
13
+ }
14
+ if (
15
+ ambiguousTypes.includes(error.name) &&
16
+ error.argument &&
17
+ error.argument.length
18
+ ) {
19
+ // no way to know what the subschema was, so don't create links for it
20
+ return error.argument
21
+ .map((s) => (s.includes('subschema') ? '' : makerFunc(s)))
22
+ .filter(Boolean);
23
+ }
24
+ return [];
25
+ };
26
+
27
+ const removeFirstAndLastChar = (s) => s.slice(1, -1);
28
+ // always return a string
29
+ const makePath = (path, newSegment) =>
30
+ (path ? [path, newSegment].join('.') : newSegment) || '';
31
+
32
+ const processBaseError = (err, path) => {
33
+ const completePath = makePath(path, err.property)
34
+ .replace(/\.instance\.?/g, '.')
35
+ .replace(/\.instance$/, '');
36
+
37
+ const subSchemas = err.message.match(/\[subschema \d+\]/g);
38
+ if (subSchemas) {
39
+ subSchemas.forEach((subschema, idx) => {
40
+ // err.schema is either an anonymous schema object or the name of a named schema
41
+ if (typeof err.schema === 'string') {
42
+ // this is basically only for FieldChoicesSchema and I'm not sure why
43
+ err.message += ' Consult the docs below for valid subschemas.';
44
+ } else {
45
+ // the subschemas have a type property
46
+ err.message = err.message.replace(
47
+ subschema,
48
+ err.schema[err.name][idx].type || 'unknown'
49
+ );
50
+ }
51
+ });
52
+ }
53
+
54
+ err.property = completePath;
55
+ return err;
56
+ };
57
+
58
+ /**
59
+ * We have a lot of `anyOf` schemas that return ambiguous errors. This recurses down the schema until it finds the errors that cause the failures replaces the ambiguity.
60
+ * @param {ValidationError} validationError an individual error
61
+ * @param {string} path current path in the error chain
62
+ * @param {Validator} validator validator object to pass around that has all the schemas
63
+ * @param {object} definition the original schema we're defining
64
+ */
65
+ const cleanError = (validationError, path, validator, definition) => {
66
+ if (ambiguousTypes.includes(validationError.name)) {
67
+ // flatObjectSchema requires each property to be a type. instead of recursing down, it's more valuable to say "hey, it's not of these types"
68
+ if (validationError.argument.every((s) => s.includes('subschema'))) {
69
+ return processBaseError(validationError, path);
70
+ }
71
+
72
+ // Try against each of A, B, and C to take a guess as to which it's closed to
73
+ // errorGroups will be an array of arrays of errors
74
+ const errorGroups = validationError.argument.map((schemaName, idx) => {
75
+ // this is what we'll validate against next
76
+ let nextSchema;
77
+ // schemaName is either "[subschema n]" or "/NamedSchema"
78
+ if (schemaName.startsWith('[subschema')) {
79
+ const maybeNamedSchema = validator.schemas[validationError.schema];
80
+
81
+ if (maybeNamedSchema) {
82
+ nextSchema = maybeNamedSchema[validationError.name][idx];
83
+ } else {
84
+ // hoist the anonymous subschema up
85
+ nextSchema = validationError.schema[validationError.name][idx];
86
+ }
87
+ } else {
88
+ nextSchema = validator.schemas[removeFirstAndLastChar(schemaName)];
89
+ }
90
+
91
+ if (validationError.instance === undefined) {
92
+ // Work around a jsonschema bug: When the value being validated is
93
+ // falsy, validationError.instance isn't available
94
+ // See https://github.com/tdegrunt/jsonschema/issues/263
95
+ const fullPath =
96
+ path.replace(/^instance\./, '') +
97
+ validationError.property.replace(/^instance\./, '');
98
+ validationError.instance = get(definition, fullPath);
99
+ }
100
+
101
+ const res = validator.validate(validationError.instance, nextSchema);
102
+
103
+ return res.errors.map((e) =>
104
+ cleanError(
105
+ e,
106
+ makePath(path, validationError.property),
107
+ validator,
108
+ definition
109
+ )
110
+ );
111
+ });
112
+
113
+ // find the group with the fewest errors, that's probably the most accurate
114
+ // if we're goign to tweak what gets returned, this is where we'll do it
115
+ // a possible improvement could be treating a longer path favorably, like the python implementation does
116
+ errorGroups.sort((a, b) => a.length - b.length);
117
+ return errorGroups[0];
118
+ } else {
119
+ // base case
120
+ return processBaseError(validationError, path);
121
+ }
122
+ };
123
+
124
+ const makeValidator = (mainSchema, subSchemas) => {
125
+ const schemas = [mainSchema].concat(subSchemas || []);
126
+ const v = new jsonschema.Validator();
127
+ schemas.forEach((Schema) => {
128
+ v.addSchema(Schema, Schema.id);
129
+ });
130
+ return {
131
+ validate: (definition) => {
132
+ const results = v.validate(definition, mainSchema);
133
+ const allErrors = results.errors.concat(
134
+ functionalConstraints.run(definition, mainSchema)
135
+ );
136
+ const cleanedErrors = flattenDeep(
137
+ allErrors.map((e) => cleanError(e, '', v, definition))
138
+ );
139
+
140
+ results.errors = cleanedErrors.map((error) => {
141
+ error.codeLinks = makeLinks(error, links.makeCodeLink);
142
+ error.docLinks = makeLinks(error, links.makeDocLink);
143
+ return error;
144
+ });
145
+ return results;
146
+ },
147
+ };
148
+ };
149
+
150
+ module.exports = makeValidator;
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "zapier-platform-schema",
3
+ "version": "11.1.1",
4
+ "description": "Schema definition for CLI apps in the Zapier Developer Platform.",
5
+ "repository": "zapier/zapier-platform",
6
+ "homepage": "https://platform.zapier.com/",
7
+ "author": "Zapier Engineering <contact@zapier.com>",
8
+ "license": "SEE LICENSE IN LICENSE",
9
+ "main": "schema.js",
10
+ "files": [
11
+ "/exported-schema.json",
12
+ "/lib/**/*.js",
13
+ "/schema.js"
14
+ ],
15
+ "scripts": {
16
+ "preversion": "git pull && yarn test && yarn build",
17
+ "test": "mocha -t 10000 --recursive test",
18
+ "smoke-test": "mocha -t 10000 --recursive smoke-test",
19
+ "test:debug": "mocha -t 5-10000 --recursive --inspect-brk test",
20
+ "lint": "eslint lib",
21
+ "lint:fix": "eslint --fix lib",
22
+ "coverage": "istanbul cover _mocha -- --recursive",
23
+ "export": "node bin/export.js && prettier --write exported-schema.json",
24
+ "docs": "node bin/docs.js",
25
+ "build": "yarn docs && yarn export",
26
+ "git-add": "git add exported-schema.json README.md docs",
27
+ "precommit": "yarn build && yarn git-add",
28
+ "validate": "yarn test && yarn lint && yarn smoke-test"
29
+ },
30
+ "dependencies": {
31
+ "jsonschema": "1.2.2",
32
+ "lodash": "4.17.21"
33
+ },
34
+ "devDependencies": {
35
+ "fs-extra": "^10.0.0",
36
+ "istanbul": "0.4.5",
37
+ "markdown-toc": "^1",
38
+ "node-fetch": "^2"
39
+ }
40
+ }
package/schema.js ADDED
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ const exportSchema = require('./lib/utils/exportSchema');
4
+
5
+ const AppSchema = require('./lib/schemas/AppSchema');
6
+
7
+ const validateAppDefinition = AppSchema.validate;
8
+
9
+ module.exports = {
10
+ AppSchema,
11
+ validateAppDefinition,
12
+ exportSchema: () => exportSchema(AppSchema),
13
+ };