@availity/dockyard 1.0.0-alpha.3 → 1.0.2

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.
package/CHANGELOG.md CHANGED
@@ -1,7 +1,31 @@
1
- # Change Log
1
+ # Changelog
2
+
3
+ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
+
5
+ ## [1.0.2](https://github.com/Availity/sdk-js/compare/@availity/dockyard@1.0.1...@availity/dockyard@1.0.2) (2022-05-24)
6
+
7
+
8
+
9
+ ## [1.0.1](https://github.com/Availity/sdk-js/compare/@availity/dockyard@1.0.0...@availity/dockyard@1.0.1) (2022-04-28)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * add browser field for output ([0ce7170](https://github.com/Availity/sdk-js/commit/0ce717075a82675b8707e4db0cc07cd4af370f3d))
15
+
16
+
17
+
18
+ # [1.0.0](https://github.com/Availity/sdk-js/compare/@availity/dockyard@1.0.0-alpha.3...@availity/dockyard@1.0.0) (2022-04-28)
19
+
20
+
21
+ * build!: switch from lerna to nx and compile to esm with tsup ([c586085](https://github.com/Availity/sdk-js/commit/c5860856ca96b743a0653d335ea00f0889132f7f))
22
+
23
+
24
+ ### BREAKING CHANGES
25
+
26
+ * Drop Internet Explorer support
27
+
2
28
 
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
29
 
6
30
  # 1.0.0-alpha.3 (2021-12-21)
7
31
 
@@ -0,0 +1 @@
1
+
package/dist/index.js ADDED
@@ -0,0 +1,122 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __spreadValues = (a, b) => {
11
+ for (var prop in b || (b = {}))
12
+ if (__hasOwnProp.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ if (__getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(b)) {
16
+ if (__propIsEnum.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ }
19
+ return a;
20
+ };
21
+ var __export = (target, all) => {
22
+ for (var name in all)
23
+ __defProp(target, name, { get: all[name], enumerable: true });
24
+ };
25
+ var __copyProps = (to, from, except, desc) => {
26
+ if (from && typeof from === "object" || typeof from === "function") {
27
+ for (let key of __getOwnPropNames(from))
28
+ if (!__hasOwnProp.call(to, key) && key !== except)
29
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
30
+ }
31
+ return to;
32
+ };
33
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
34
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
35
+
36
+ // src/index.js
37
+ var src_exports = {};
38
+ __export(src_exports, {
39
+ default: () => src_default
40
+ });
41
+ module.exports = __toCommonJS(src_exports);
42
+ var import_get = __toESM(require("lodash/get"));
43
+ var import_set = __toESM(require("lodash/set"));
44
+ var addDelimiter = (a, b, { underscore = false } = {}) => underscore ? a ? `${a}.${b}._${b}` : `${b}._${b}` : a ? `${a}.${b}` : `${b}`;
45
+ var transformRules = (schemaFieldDocs, options) => {
46
+ var _a, _b, _c;
47
+ const fieldDocs = [];
48
+ let isRequired = false;
49
+ if (!options.excludeTypes && schemaFieldDocs.type) {
50
+ fieldDocs.push(schemaFieldDocs.type);
51
+ }
52
+ if (((_a = schemaFieldDocs.notOneOf) == null ? void 0 : _a.length) > 0) {
53
+ fieldDocs.push(`not one of: [${schemaFieldDocs.notOneOf.join(", ")}]`);
54
+ }
55
+ if (!options.excludeOneOf && ((_b = schemaFieldDocs.oneOf) == null ? void 0 : _b.length) > 0) {
56
+ fieldDocs.push(`one of: [${schemaFieldDocs.oneOf.join(", ")}]`);
57
+ }
58
+ if (((_c = schemaFieldDocs.tests) == null ? void 0 : _c.length) > 0) {
59
+ for (const test of schemaFieldDocs.tests) {
60
+ if (options.compileRequiredFields && test.name === "required") {
61
+ isRequired = true;
62
+ } else if (test.params) {
63
+ const params = [];
64
+ for (const param in test.params) {
65
+ if (Object.prototype.hasOwnProperty.call(test.params, param)) {
66
+ params.push(`${param} ${test.params[param]}`);
67
+ }
68
+ }
69
+ if (test.name === "max" || test.name === "min") {
70
+ fieldDocs.push(`${params.join(", ")}${schemaFieldDocs.type !== "number" && schemaFieldDocs.type !== "array" ? " chars" : ""}`);
71
+ } else {
72
+ fieldDocs.push(`${test.name} ${params.join(", ")}`);
73
+ }
74
+ } else {
75
+ fieldDocs.push(test.name);
76
+ }
77
+ }
78
+ }
79
+ const friendlyFieldDocs = fieldDocs.length > 0 ? `Rules: ${fieldDocs.join(", ")}.` : "";
80
+ return options.compileRequiredFields ? { description: friendlyFieldDocs, isRequired } : friendlyFieldDocs;
81
+ };
82
+ var buildRules = (fields, head = "", options) => Object.entries(fields).reduce((obj, [key, value]) => {
83
+ const pathOptions = {};
84
+ if (value.fields || value.innerType && value.innerType.fields) {
85
+ pathOptions.underscore = true;
86
+ }
87
+ const path = addDelimiter(head, key, pathOptions);
88
+ const rules = transformRules(value, options);
89
+ if (options.compileRequiredFields) {
90
+ (0, import_set.default)(obj, path, rules.description);
91
+ if (rules.isRequired) {
92
+ const requiredFieldName = head ? `${head}.${key}` : key;
93
+ (0, import_set.default)(obj, "requiredFields", obj.requiredFields ? [...obj.requiredFields, requiredFieldName] : [requiredFieldName]);
94
+ }
95
+ } else {
96
+ (0, import_set.default)(obj, path, rules);
97
+ }
98
+ if (value == null ? void 0 : value.fields) {
99
+ const subFieldHead = addDelimiter(head, key);
100
+ const subRules = buildRules(value.fields, subFieldHead, options);
101
+ (0, import_set.default)(obj, subFieldHead, __spreadValues(__spreadValues({}, (0, import_get.default)(obj, subFieldHead)), (0, import_get.default)(subRules, subFieldHead)));
102
+ if (options.compileRequiredFields && subRules.requiredFields) {
103
+ (0, import_set.default)(obj, "requiredFields", obj.requiredFields ? [...obj.requiredFields, ...subRules.requiredFields] : [...subRules.requiredFields]);
104
+ }
105
+ }
106
+ if (value.innerType && value.innerType.fields) {
107
+ const innerFieldHead = addDelimiter(head, key);
108
+ const innerRules = buildRules(value.innerType.fields, innerFieldHead, options);
109
+ (0, import_set.default)(obj, innerFieldHead, __spreadValues(__spreadValues({}, (0, import_get.default)(obj, innerFieldHead)), (0, import_get.default)(innerRules, innerFieldHead)));
110
+ if (options.compileRequiredFields && innerRules.requiredFields) {
111
+ (0, import_set.default)(obj, "requiredFields", obj.requiredFields ? [...obj.requiredFields, ...innerRules.requiredFields] : [...innerRules.requiredFields]);
112
+ }
113
+ }
114
+ return obj;
115
+ }, {});
116
+ var getRules = (validation, { compileRequiredFields = false, excludeOneOf = false, excludeTypes = false } = {}) => {
117
+ const description = validation.describe();
118
+ return buildRules(description.fields, "", { compileRequiredFields, excludeOneOf, excludeTypes });
119
+ };
120
+ var src_default = getRules;
121
+ // Annotate the CommonJS export names for ESM import in node:
122
+ 0 && (module.exports = {});
package/dist/index.mjs ADDED
@@ -0,0 +1,100 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+
18
+ // src/index.js
19
+ import get from "lodash/get";
20
+ import set from "lodash/set";
21
+ var addDelimiter = (a, b, { underscore = false } = {}) => underscore ? a ? `${a}.${b}._${b}` : `${b}._${b}` : a ? `${a}.${b}` : `${b}`;
22
+ var transformRules = (schemaFieldDocs, options) => {
23
+ var _a, _b, _c;
24
+ const fieldDocs = [];
25
+ let isRequired = false;
26
+ if (!options.excludeTypes && schemaFieldDocs.type) {
27
+ fieldDocs.push(schemaFieldDocs.type);
28
+ }
29
+ if (((_a = schemaFieldDocs.notOneOf) == null ? void 0 : _a.length) > 0) {
30
+ fieldDocs.push(`not one of: [${schemaFieldDocs.notOneOf.join(", ")}]`);
31
+ }
32
+ if (!options.excludeOneOf && ((_b = schemaFieldDocs.oneOf) == null ? void 0 : _b.length) > 0) {
33
+ fieldDocs.push(`one of: [${schemaFieldDocs.oneOf.join(", ")}]`);
34
+ }
35
+ if (((_c = schemaFieldDocs.tests) == null ? void 0 : _c.length) > 0) {
36
+ for (const test of schemaFieldDocs.tests) {
37
+ if (options.compileRequiredFields && test.name === "required") {
38
+ isRequired = true;
39
+ } else if (test.params) {
40
+ const params = [];
41
+ for (const param in test.params) {
42
+ if (Object.prototype.hasOwnProperty.call(test.params, param)) {
43
+ params.push(`${param} ${test.params[param]}`);
44
+ }
45
+ }
46
+ if (test.name === "max" || test.name === "min") {
47
+ fieldDocs.push(`${params.join(", ")}${schemaFieldDocs.type !== "number" && schemaFieldDocs.type !== "array" ? " chars" : ""}`);
48
+ } else {
49
+ fieldDocs.push(`${test.name} ${params.join(", ")}`);
50
+ }
51
+ } else {
52
+ fieldDocs.push(test.name);
53
+ }
54
+ }
55
+ }
56
+ const friendlyFieldDocs = fieldDocs.length > 0 ? `Rules: ${fieldDocs.join(", ")}.` : "";
57
+ return options.compileRequiredFields ? { description: friendlyFieldDocs, isRequired } : friendlyFieldDocs;
58
+ };
59
+ var buildRules = (fields, head = "", options) => Object.entries(fields).reduce((obj, [key, value]) => {
60
+ const pathOptions = {};
61
+ if (value.fields || value.innerType && value.innerType.fields) {
62
+ pathOptions.underscore = true;
63
+ }
64
+ const path = addDelimiter(head, key, pathOptions);
65
+ const rules = transformRules(value, options);
66
+ if (options.compileRequiredFields) {
67
+ set(obj, path, rules.description);
68
+ if (rules.isRequired) {
69
+ const requiredFieldName = head ? `${head}.${key}` : key;
70
+ set(obj, "requiredFields", obj.requiredFields ? [...obj.requiredFields, requiredFieldName] : [requiredFieldName]);
71
+ }
72
+ } else {
73
+ set(obj, path, rules);
74
+ }
75
+ if (value == null ? void 0 : value.fields) {
76
+ const subFieldHead = addDelimiter(head, key);
77
+ const subRules = buildRules(value.fields, subFieldHead, options);
78
+ set(obj, subFieldHead, __spreadValues(__spreadValues({}, get(obj, subFieldHead)), get(subRules, subFieldHead)));
79
+ if (options.compileRequiredFields && subRules.requiredFields) {
80
+ set(obj, "requiredFields", obj.requiredFields ? [...obj.requiredFields, ...subRules.requiredFields] : [...subRules.requiredFields]);
81
+ }
82
+ }
83
+ if (value.innerType && value.innerType.fields) {
84
+ const innerFieldHead = addDelimiter(head, key);
85
+ const innerRules = buildRules(value.innerType.fields, innerFieldHead, options);
86
+ set(obj, innerFieldHead, __spreadValues(__spreadValues({}, get(obj, innerFieldHead)), get(innerRules, innerFieldHead)));
87
+ if (options.compileRequiredFields && innerRules.requiredFields) {
88
+ set(obj, "requiredFields", obj.requiredFields ? [...obj.requiredFields, ...innerRules.requiredFields] : [...innerRules.requiredFields]);
89
+ }
90
+ }
91
+ return obj;
92
+ }, {});
93
+ var getRules = (validation, { compileRequiredFields = false, excludeOneOf = false, excludeTypes = false } = {}) => {
94
+ const description = validation.describe();
95
+ return buildRules(description.fields, "", { compileRequiredFields, excludeOneOf, excludeTypes });
96
+ };
97
+ var src_default = getRules;
98
+ export {
99
+ src_default as default
100
+ };
package/jest.config.js ADDED
@@ -0,0 +1,7 @@
1
+ const global = require('../../jest.config');
2
+
3
+ module.exports = {
4
+ ...global,
5
+ displayName: 'dockyard',
6
+ coverageDirectory: '../../coverage/dockyard',
7
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@availity/dockyard",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.2",
4
4
  "description": "Convert yup schema to a friendly docs object",
5
5
  "keywords": [
6
6
  "yup",
@@ -17,23 +17,28 @@
17
17
  },
18
18
  "license": "MIT",
19
19
  "author": "Greg Martin <greg.martin@availity.com>",
20
- "main": "lib/dockyard.js",
21
- "module": "src/dockyard.js",
20
+ "browser": "./dist/index.js",
21
+ "main": "./dist/index.js",
22
+ "module": "./dist/index.mjs",
23
+ "types": "./dist/index.d.ts",
22
24
  "scripts": {
23
- "build": "yarn clean && yarn transpile",
24
- "clean": "rimraf ./lib",
25
- "transpile": "babel --root-mode upward src -d lib --ignore **/*.test.js"
25
+ "build": "tsup src/index.js --format esm,cjs --dts",
26
+ "dev": "tsup src/index.js --format esm,cjs --watch --dts",
27
+ "lint": "eslint src",
28
+ "lint:fix": "eslint src --fix",
29
+ "clean": "rm -rf node_modules && rm -rf dist",
30
+ "bundlesize": "bundlesize",
31
+ "publish": "yarn npm publish --tolerate-republish --access public"
26
32
  },
27
33
  "dependencies": {
28
- "@babel/runtime": "^7.16.5",
29
- "core-js": "^3.19.0",
30
34
  "lodash": "^4.17.21"
31
35
  },
32
36
  "devDependencies": {
37
+ "tsup": "^5.12.8",
38
+ "typescript": "^4.6.4",
33
39
  "yup": "^0.32.11"
34
40
  },
35
41
  "publishConfig": {
36
42
  "access": "public"
37
- },
38
- "gitHead": "1c3e8c8045b16fc5111f941d863600f0a97cdbff"
39
- }
43
+ }
44
+ }
package/project.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "root": "packages/dockyard",
3
+ "projectType": "library",
4
+ "targets": {
5
+ "test": {
6
+ "executor": "@nrwl/jest:jest",
7
+ "outputs": ["coverage/dockyard"],
8
+ "options": {
9
+ "jestConfig": "packages/dockyard/jest.config.js",
10
+ "passWithNoTests": true
11
+ }
12
+ },
13
+ "version": {
14
+ "executor": "@jscutlery/semver:version",
15
+ "options": {
16
+ "preset": "angular",
17
+ "commitMessageFormat": "chore(${projectName}): release version ${version} [skip ci]",
18
+ "tagPrefix": "@availity/${projectName}@",
19
+ "baseBranch": "master"
20
+ }
21
+ },
22
+ "lint": {
23
+ "executor": "@nrwl/linter:eslint",
24
+ "options": {
25
+ "eslintConfig": ".eslintrc.yaml",
26
+ "lintFilePatterns": ["packages/dockyard/**/*.{js,ts}"],
27
+ "silent": false,
28
+ "fix": false,
29
+ "cache": true,
30
+ "cacheLocation": "./node_modules/.cache/dockyard/.eslintcache",
31
+ "maxWarnings": -1,
32
+ "quiet": false,
33
+ "noEslintrc": false,
34
+ "hasTypeAwareRules": true,
35
+ "cacheStrategy": "metadata"
36
+ }
37
+ }
38
+ }
39
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ declare const getRules: (
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ validation: any,
4
+ {
5
+ compileRequiredFields,
6
+ excludeOneOf,
7
+ excludeTypes,
8
+ }?: {
9
+ compileRequiredFields: boolean;
10
+ excludeOneOf: boolean;
11
+ excludeTypes: boolean;
12
+ }
13
+ ) => Record<string, unknown>;
@@ -1,5 +1,5 @@
1
- const get = require('lodash/get');
2
- const set = require('lodash/set');
1
+ import get from 'lodash/get';
2
+ import set from 'lodash/set';
3
3
 
4
4
  /**
5
5
  * Builds the object path of the description
@@ -141,4 +141,4 @@ const getRules = (validation, { compileRequiredFields = false, excludeOneOf = fa
141
141
  return buildRules(description.fields, '', { compileRequiredFields, excludeOneOf, excludeTypes });
142
142
  };
143
143
 
144
- module.exports = getRules;
144
+ export default getRules;
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "include": ["."],
4
+ "exclude": ["dist", "build", "node_modules"]
5
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../dist/out-tsc",
5
+ "module": "commonjs",
6
+ "types": ["jest", "node"],
7
+ "allowJs": true
8
+ },
9
+ "include": ["**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"]
10
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2016-present Availity, LLC
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/lib/dockyard.js DELETED
@@ -1,214 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- require("core-js/modules/es.array.concat");
6
-
7
- var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
8
-
9
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
-
11
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
12
-
13
- var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));
14
-
15
- require("core-js/modules/es.array.join.js");
16
-
17
- require("core-js/modules/es.function.name.js");
18
-
19
- require("core-js/modules/es.object.to-string.js");
20
-
21
- require("core-js/modules/esnext.async-iterator.reduce.js");
22
-
23
- require("core-js/modules/esnext.iterator.constructor.js");
24
-
25
- require("core-js/modules/esnext.iterator.reduce.js");
26
-
27
- require("core-js/modules/es.object.entries.js");
28
-
29
- require("core-js/modules/es.symbol.js");
30
-
31
- require("core-js/modules/es.symbol.description.js");
32
-
33
- var get = require('lodash/get');
34
-
35
- var set = require('lodash/set');
36
- /**
37
- * Builds the object path of the description
38
- * @param {string} a
39
- * @param {string} b
40
- * @param {Object} options
41
- * @param {boolean} [options.underscore = false] - Set object path for an object ex. object._object
42
- * @returns
43
- */
44
-
45
-
46
- var addDelimiter = function addDelimiter(a, b) {
47
- var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
48
- _ref$underscore = _ref.underscore,
49
- underscore = _ref$underscore === void 0 ? false : _ref$underscore;
50
-
51
- return underscore ? a ? "".concat(a, ".").concat(b, "._").concat(b) : "".concat(b, "._").concat(b) : a ? "".concat(a, ".").concat(b) : "".concat(b);
52
- };
53
- /**
54
- * Transforms a field's SchemaDescription object to friendly docs
55
- * @param {*} schemaFieldDocs yup schema field
56
- * @param {Object} options
57
- * @param {boolean} options.compileRequiredFields - removes the word 'required' from the description and adds an array of required fields to the object
58
- * @param {boolean} options.excludeOneOf - if oneOf is specified on an item, exclude it from the description
59
- * @param {boolean} options.excludeTypes - exclude types from the description
60
- * @returns
61
- */
62
-
63
-
64
- var transformRules = function transformRules(schemaFieldDocs, options) {
65
- var _schemaFieldDocs$notO, _schemaFieldDocs$oneO, _schemaFieldDocs$test;
66
-
67
- var fieldDocs = [];
68
- var isRequired = false;
69
-
70
- if (!options.excludeTypes && schemaFieldDocs.type) {
71
- fieldDocs.push(schemaFieldDocs.type);
72
- }
73
-
74
- if (((_schemaFieldDocs$notO = schemaFieldDocs.notOneOf) === null || _schemaFieldDocs$notO === void 0 ? void 0 : _schemaFieldDocs$notO.length) > 0) {
75
- fieldDocs.push("not one of: [".concat(schemaFieldDocs.notOneOf.join(', '), "]"));
76
- }
77
-
78
- if (!options.excludeOneOf && ((_schemaFieldDocs$oneO = schemaFieldDocs.oneOf) === null || _schemaFieldDocs$oneO === void 0 ? void 0 : _schemaFieldDocs$oneO.length) > 0) {
79
- fieldDocs.push("one of: [".concat(schemaFieldDocs.oneOf.join(', '), "]"));
80
- }
81
-
82
- if (((_schemaFieldDocs$test = schemaFieldDocs.tests) === null || _schemaFieldDocs$test === void 0 ? void 0 : _schemaFieldDocs$test.length) > 0) {
83
- var _iterator = (0, _createForOfIteratorHelper2["default"])(schemaFieldDocs.tests),
84
- _step;
85
-
86
- try {
87
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
88
- var test = _step.value;
89
-
90
- if (options.compileRequiredFields && test.name === 'required') {
91
- isRequired = true;
92
- } else if (test.params) {
93
- var params = [];
94
-
95
- for (var param in test.params) {
96
- if (Object.prototype.hasOwnProperty.call(test.params, param)) {
97
- params.push("".concat(param, " ").concat(test.params[param]));
98
- }
99
- }
100
-
101
- if (test.name === 'max' || test.name === 'min') {
102
- fieldDocs.push("".concat(params.join(', ')).concat(schemaFieldDocs.type !== 'number' && schemaFieldDocs.type !== 'array' ? ' chars' : ''));
103
- } else {
104
- fieldDocs.push("".concat(test.name, " ").concat(params.join(', ')));
105
- }
106
- } else {
107
- fieldDocs.push(test.name);
108
- }
109
- }
110
- } catch (err) {
111
- _iterator.e(err);
112
- } finally {
113
- _iterator.f();
114
- }
115
- }
116
-
117
- var friendlyFieldDocs = fieldDocs.length > 0 ? "Rules: ".concat(fieldDocs.join(', '), ".") : '';
118
- return options.compileRequiredFields ? {
119
- description: friendlyFieldDocs,
120
- isRequired: isRequired
121
- } : friendlyFieldDocs;
122
- };
123
- /**
124
- * Loops through the SchemaDescription and assigns descriptions
125
- * @param {*} fields - yup schema fields
126
- * @param {string} head - string
127
- * @param {Object} options
128
- * @param {boolean} options.compileRequiredFields - removes the word 'required' from the description and adds an array of required fields to the object
129
- * @param {boolean} options.excludeOneOf - if oneOf is specified on an item, exclude it from the description
130
- * @param {boolean} options.excludeTypes - exclude types from the description
131
- * @returns
132
- */
133
-
134
-
135
- var buildRules = function buildRules(fields) {
136
- var head = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
137
- var options = arguments.length > 2 ? arguments[2] : undefined;
138
- return Object.entries(fields).reduce(function (obj, _ref2) {
139
- var _ref3 = (0, _slicedToArray2["default"])(_ref2, 2),
140
- key = _ref3[0],
141
- value = _ref3[1];
142
-
143
- var pathOptions = {};
144
-
145
- if (value.fields || value.innerType && value.innerType.fields) {
146
- pathOptions.underscore = true;
147
- }
148
-
149
- var path = addDelimiter(head, key, pathOptions);
150
- var rules = transformRules(value, options);
151
-
152
- if (options.compileRequiredFields) {
153
- set(obj, path, rules.description);
154
-
155
- if (rules.isRequired) {
156
- var requiredFieldName = head ? "".concat(head, ".").concat(key) : key;
157
- set(obj, 'requiredFields', obj.requiredFields ? [].concat((0, _toConsumableArray2["default"])(obj.requiredFields), [requiredFieldName]) : [requiredFieldName]);
158
- }
159
- } else {
160
- set(obj, path, rules);
161
- }
162
-
163
- if (value === null || value === void 0 ? void 0 : value.fields) {
164
- var subFieldHead = addDelimiter(head, key);
165
- var subRules = buildRules(value.fields, subFieldHead, options);
166
- set(obj, subFieldHead, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, get(obj, subFieldHead)), get(subRules, subFieldHead)));
167
-
168
- if (options.compileRequiredFields && subRules.requiredFields) {
169
- set(obj, 'requiredFields', obj.requiredFields ? [].concat((0, _toConsumableArray2["default"])(obj.requiredFields), (0, _toConsumableArray2["default"])(subRules.requiredFields)) : (0, _toConsumableArray2["default"])(subRules.requiredFields));
170
- }
171
- }
172
-
173
- if (value.innerType && value.innerType.fields) {
174
- var innerFieldHead = addDelimiter(head, key);
175
- var innerRules = buildRules(value.innerType.fields, innerFieldHead, options);
176
- set(obj, innerFieldHead, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, get(obj, innerFieldHead)), get(innerRules, innerFieldHead)));
177
-
178
- if (options.compileRequiredFields && innerRules.requiredFields) {
179
- set(obj, 'requiredFields', obj.requiredFields ? [].concat((0, _toConsumableArray2["default"])(obj.requiredFields), (0, _toConsumableArray2["default"])(innerRules.requiredFields)) : (0, _toConsumableArray2["default"])(innerRules.requiredFields));
180
- }
181
- }
182
-
183
- return obj;
184
- }, {});
185
- };
186
- /**
187
- * Convert yup schema to a friendly docs object
188
- * @param {*} validation - yup validation schema
189
- * @param {Object} options
190
- * @param {boolean} options.compileRequiredFields - removes the word 'required' from the description and adds an array of required fields to the object
191
- * @param {boolean} options.excludeOneOf - if oneOf is specified on an item, exclude it from the description
192
- * @param {boolean} options.excludeTypes - exclude types from the description
193
- * @returns
194
- */
195
-
196
-
197
- var getRules = function getRules(validation) {
198
- var _ref4 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
199
- _ref4$compileRequired = _ref4.compileRequiredFields,
200
- compileRequiredFields = _ref4$compileRequired === void 0 ? false : _ref4$compileRequired,
201
- _ref4$excludeOneOf = _ref4.excludeOneOf,
202
- excludeOneOf = _ref4$excludeOneOf === void 0 ? false : _ref4$excludeOneOf,
203
- _ref4$excludeTypes = _ref4.excludeTypes,
204
- excludeTypes = _ref4$excludeTypes === void 0 ? false : _ref4$excludeTypes;
205
-
206
- var description = validation.describe();
207
- return buildRules(description.fields, '', {
208
- compileRequiredFields: compileRequiredFields,
209
- excludeOneOf: excludeOneOf,
210
- excludeTypes: excludeTypes
211
- });
212
- };
213
-
214
- module.exports = getRules;
package/tests/schemas.js DELETED
@@ -1,33 +0,0 @@
1
- const yup = require('yup');
2
-
3
- const movieSchema = yup.object({
4
- name: yup.string().matches('/^[w-]*$/').required(),
5
- job: yup.string().max(250).notOneOf(['critic', 'blogger', 'influencer']),
6
- friends: yup
7
- .object({
8
- name: yup.string().matches('/^[w-]*$/').required(),
9
- job: yup.string().max(250).oneOf(['critic', 'blogger', 'influencer']),
10
- favoriteMovie: yup
11
- .object({
12
- name: yup.string().min(1).required(),
13
- rating: yup.number().min(1).max(5),
14
- awards: yup.object({
15
- bestMovie: yup.boolean().required(),
16
- bestActor: yup.boolean().required(),
17
- }),
18
- actors: yup
19
- .array(
20
- yup.object({
21
- name: yup.string().max(100).required(),
22
- birthDate: yup.date(),
23
- })
24
- )
25
- .max(10),
26
- })
27
- .required(),
28
- })
29
- .noUnknown()
30
- .required(),
31
- });
32
-
33
- module.exports = { movieSchema };