@elliemae/ds-props-helpers 1.60.0 → 2.0.0-alpha.12
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/cjs/defaultProps/index.js +2 -6
- package/cjs/defaultProps/useMemoMergePropsWithDefault.js +13 -9
- package/cjs/getProps/index.js +13 -23
- package/cjs/index.js +13 -21
- package/cjs/validation/errorTemplates.js +4 -9
- package/cjs/validation/index.js +8 -14
- package/cjs/validation/typescriptGuards.js +16 -29
- package/cjs/validation/typescriptParsers.js +12 -9
- package/cjs/validation/typescriptValidator.js +77 -72
- package/cjs/validation/validator.js +13 -18
- package/esm/defaultProps/index.js +0 -4
- package/esm/defaultProps/useMemoMergePropsWithDefault.js +12 -8
- package/esm/getProps/index.js +13 -19
- package/esm/index.js +0 -8
- package/esm/validation/errorTemplates.js +4 -5
- package/esm/validation/index.js +0 -6
- package/esm/validation/typescriptGuards.js +16 -29
- package/esm/validation/typescriptParsers.js +12 -9
- package/esm/validation/typescriptValidator.js +55 -45
- package/esm/validation/validator.js +10 -11
- package/package.json +70 -10
- package/types/defaultProps/index.d.ts +1 -0
- package/types/defaultProps/useMemoMergePropsWithDefault.d.ts +1 -0
- package/types/getProps/index.d.ts +2 -0
- package/types/index.d.ts +3 -0
- package/types/tests/any.validation.test.d.ts +1 -0
- package/types/tests/array.validation.test.d.ts +1 -0
- package/types/tests/boolean.validation.test.d.ts +1 -0
- package/types/tests/function.validation.test.d.ts +1 -0
- package/types/tests/number.validation.test.d.ts +1 -0
- package/types/tests/object.validation.test.d.ts +1 -0
- package/types/tests/schema.validation.test.d.ts +1 -0
- package/types/tests/string.validation.test.d.ts +1 -0
- package/types/tests/test.schema.d.ts +1 -0
- package/types/tests/union.validation.test.d.ts +1 -0
- package/types/validation/errorTemplates.d.ts +2 -0
- package/types/validation/index.d.ts +3 -0
- package/types/validation/typescriptGuards.d.ts +11 -0
- package/types/validation/typescriptParsers.d.ts +1 -0
- package/types/validation/typescriptValidator.d.ts +15 -0
- package/types/validation/validator.d.ts +1 -0
- package/cjs/defaultProps/index.js.map +0 -1
- package/cjs/defaultProps/useMemoMergePropsWithDefault.js.map +0 -1
- package/cjs/getProps/index.js.map +0 -1
- package/cjs/index.js.map +0 -1
- package/cjs/tests/test.schema.js +0 -43
- package/cjs/tests/test.schema.js.map +0 -1
- package/cjs/validation/errorTemplates.js.map +0 -1
- package/cjs/validation/index.js.map +0 -1
- package/cjs/validation/typescriptGuards.js.map +0 -1
- package/cjs/validation/typescriptParsers.js.map +0 -1
- package/cjs/validation/typescriptValidator.js.map +0 -1
- package/cjs/validation/validator.js.map +0 -1
- package/defaultProps/package.json +0 -10
- package/defaultProps/useMemoMergePropsWithDefault/package.json +0 -10
- package/esm/defaultProps/index.js.map +0 -1
- package/esm/defaultProps/useMemoMergePropsWithDefault.js.map +0 -1
- package/esm/getProps/index.js.map +0 -1
- package/esm/index.js.map +0 -1
- package/esm/tests/test.schema.js +0 -39
- package/esm/tests/test.schema.js.map +0 -1
- package/esm/validation/errorTemplates.js.map +0 -1
- package/esm/validation/index.js.map +0 -1
- package/esm/validation/typescriptGuards.js.map +0 -1
- package/esm/validation/typescriptParsers.js.map +0 -1
- package/esm/validation/typescriptValidator.js.map +0 -1
- package/esm/validation/validator.js.map +0 -1
- package/getProps/package.json +0 -10
- package/tests/test.schema/package.json +0 -10
- package/validation/errorTemplates/package.json +0 -10
- package/validation/package.json +0 -10
- package/validation/typescriptGuards/package.json +0 -10
- package/validation/typescriptParsers/package.json +0 -10
- package/validation/typescriptValidator/package.json +0 -10
- package/validation/validator/package.json +0 -10
|
@@ -2,12 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
require('@babel/runtime/helpers/defineProperty');
|
|
7
|
-
require('react');
|
|
8
|
-
require('fast-deep-equal/react');
|
|
5
|
+
var useMemoMergePropsWithDefault = require('./useMemoMergePropsWithDefault.js');
|
|
9
6
|
|
|
10
7
|
|
|
11
8
|
|
|
12
|
-
exports.useMemoMergePropsWithDefault =
|
|
13
|
-
//# sourceMappingURL=index.js.map
|
|
9
|
+
exports.useMemoMergePropsWithDefault = useMemoMergePropsWithDefault.useMemoMergePropsWithDefault;
|
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
require('core-js/modules/esnext.async-iterator.filter.js');
|
|
6
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.filter.js');
|
|
8
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
9
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
5
10
|
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
6
11
|
var react = require('react');
|
|
7
12
|
var deepequal = require('fast-deep-equal/react');
|
|
@@ -13,16 +18,16 @@ var deepequal__default = /*#__PURE__*/_interopDefaultLegacy(deepequal);
|
|
|
13
18
|
|
|
14
19
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
15
20
|
|
|
16
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty__default[
|
|
21
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
17
22
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
23
|
|
|
19
|
-
|
|
24
|
+
const useMemoCompare = (next, compare) => {
|
|
20
25
|
// Ref for storing previous value
|
|
21
|
-
|
|
22
|
-
|
|
26
|
+
const previousRef = react.useRef(next);
|
|
27
|
+
const previous = previousRef.current; // Pass previous and next value to compare function
|
|
23
28
|
// to determine whether to consider them equal.
|
|
24
29
|
|
|
25
|
-
|
|
30
|
+
const isEqual = compare(previous, next); // If not equal update previousRef to next value.
|
|
26
31
|
// We only update if not equal so that this hook continues to return
|
|
27
32
|
// the same old value if compare keeps returning true.
|
|
28
33
|
|
|
@@ -32,13 +37,12 @@ var useMemoCompare = function useMemoCompare(next, compare) {
|
|
|
32
37
|
}; // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
38
|
|
|
34
39
|
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
const useMemoMergePropsWithDefault = function (props, defaultProps) {
|
|
41
|
+
let compare = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : deepequal__default["default"];
|
|
37
42
|
|
|
38
|
-
|
|
43
|
+
const mergedProps = _objectSpread(_objectSpread({}, defaultProps), props);
|
|
39
44
|
|
|
40
45
|
return useMemoCompare(mergedProps, compare);
|
|
41
46
|
};
|
|
42
47
|
|
|
43
48
|
exports.useMemoMergePropsWithDefault = useMemoMergePropsWithDefault;
|
|
44
|
-
//# sourceMappingURL=useMemoMergePropsWithDefault.js.map
|
package/cjs/getProps/index.js
CHANGED
|
@@ -2,29 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
var getDataProps = function getDataProps(props) {
|
|
20
|
-
return Object.fromEntries(Object.entries(props).filter(function (_ref3) {
|
|
21
|
-
var _ref4 = _slicedToArray__default['default'](_ref3, 1),
|
|
22
|
-
key = _ref4[0];
|
|
23
|
-
|
|
24
|
-
return key.includes('data-');
|
|
25
|
-
}));
|
|
26
|
-
};
|
|
5
|
+
require('core-js/modules/esnext.async-iterator.filter.js');
|
|
6
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.filter.js');
|
|
8
|
+
require('core-js/modules/web.dom-collections.iterator.js');
|
|
9
|
+
|
|
10
|
+
const getAriaProps = props => Object.fromEntries(Object.entries(props).filter(_ref => {
|
|
11
|
+
let [key] = _ref;
|
|
12
|
+
return key.includes('aria-');
|
|
13
|
+
}));
|
|
14
|
+
const getDataProps = props => Object.fromEntries(Object.entries(props).filter(_ref2 => {
|
|
15
|
+
let [key] = _ref2;
|
|
16
|
+
return key.includes('data-');
|
|
17
|
+
}));
|
|
27
18
|
|
|
28
19
|
exports.getAriaProps = getAriaProps;
|
|
29
20
|
exports.getDataProps = getDataProps;
|
|
30
|
-
//# sourceMappingURL=index.js.map
|
package/cjs/index.js
CHANGED
|
@@ -2,27 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
require('@babel/runtime/helpers/defineProperty');
|
|
11
|
-
require('react');
|
|
12
|
-
require('fast-deep-equal/react');
|
|
13
|
-
require('@babel/runtime/helpers/typeof');
|
|
14
|
-
require('@babel/runtime/helpers/slicedToArray');
|
|
15
|
-
require('./validation/typescriptGuards.js');
|
|
16
|
-
require('./validation/typescriptParsers.js');
|
|
5
|
+
var useMemoMergePropsWithDefault = require('./defaultProps/useMemoMergePropsWithDefault.js');
|
|
6
|
+
var validator = require('./validation/validator.js');
|
|
7
|
+
var errorTemplates = require('./validation/errorTemplates.js');
|
|
8
|
+
var typescriptValidator = require('./validation/typescriptValidator.js');
|
|
9
|
+
var index = require('./getProps/index.js');
|
|
17
10
|
|
|
18
11
|
|
|
19
12
|
|
|
20
|
-
exports.useMemoMergePropsWithDefault =
|
|
21
|
-
exports.useValidatePropTypes =
|
|
22
|
-
exports.throwRequiredError =
|
|
23
|
-
exports.throwTypeError =
|
|
24
|
-
exports.useValidateTypescriptPropTypes =
|
|
25
|
-
exports.validateTypescriptPropTypesImplementation =
|
|
26
|
-
exports.getAriaProps =
|
|
27
|
-
exports.getDataProps =
|
|
28
|
-
//# sourceMappingURL=index.js.map
|
|
13
|
+
exports.useMemoMergePropsWithDefault = useMemoMergePropsWithDefault.useMemoMergePropsWithDefault;
|
|
14
|
+
exports.useValidatePropTypes = validator.useValidatePropTypes;
|
|
15
|
+
exports.throwRequiredError = errorTemplates.throwRequiredError;
|
|
16
|
+
exports.throwTypeError = errorTemplates.throwTypeError;
|
|
17
|
+
exports.useValidateTypescriptPropTypes = typescriptValidator.useValidateTypescriptPropTypes;
|
|
18
|
+
exports.validateTypescriptPropTypesImplementation = typescriptValidator.validateTypescriptPropTypesImplementation;
|
|
19
|
+
exports.getAriaProps = index.getAriaProps;
|
|
20
|
+
exports.getDataProps = index.getDataProps;
|
|
@@ -2,20 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
-
|
|
9
|
-
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
|
|
5
|
+
require('core-js/modules/es.string.replace.js');
|
|
10
6
|
|
|
11
7
|
/* eslint-disable max-params */
|
|
12
|
-
|
|
13
|
-
throw new Error("".concat(componentName, ":: You are trying to pass a not valid \"").concat(validPropKey, "\" property, \n please provide a valid type.\n\n Received: ").concat(invalidProp, " (").concat(
|
|
8
|
+
const throwTypeError = (componentName, validPropKey, invalidProp, validFormat) => {
|
|
9
|
+
throw new Error("".concat(componentName, ":: You are trying to pass a not valid \"").concat(validPropKey, "\" property, \n please provide a valid type.\n\n Received: ").concat(invalidProp, " (").concat(typeof invalidProp, ")\n Expected: (").concat(validFormat.replace('\n', ' or '), ")\n "));
|
|
14
10
|
};
|
|
15
|
-
|
|
11
|
+
const throwRequiredError = (componentName, validPropKey) => {
|
|
16
12
|
throw new Error("".concat(componentName, ":: Please provide a/an \"").concat(validPropKey, "\" property to use this component. \n This property is required.\n "));
|
|
17
13
|
};
|
|
18
14
|
|
|
19
15
|
exports.throwRequiredError = throwRequiredError;
|
|
20
16
|
exports.throwTypeError = throwTypeError;
|
|
21
|
-
//# sourceMappingURL=errorTemplates.js.map
|
package/cjs/validation/index.js
CHANGED
|
@@ -2,20 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
require('@babel/runtime/helpers/typeof');
|
|
9
|
-
require('@babel/runtime/helpers/slicedToArray');
|
|
10
|
-
require('react');
|
|
11
|
-
require('./typescriptGuards.js');
|
|
12
|
-
require('./typescriptParsers.js');
|
|
5
|
+
var validator = require('./validator.js');
|
|
6
|
+
var errorTemplates = require('./errorTemplates.js');
|
|
7
|
+
var typescriptValidator = require('./typescriptValidator.js');
|
|
13
8
|
|
|
14
9
|
|
|
15
10
|
|
|
16
|
-
exports.useValidatePropTypes =
|
|
17
|
-
exports.throwRequiredError =
|
|
18
|
-
exports.throwTypeError =
|
|
19
|
-
exports.useValidateTypescriptPropTypes =
|
|
20
|
-
exports.validateTypescriptPropTypesImplementation =
|
|
21
|
-
//# sourceMappingURL=index.js.map
|
|
11
|
+
exports.useValidatePropTypes = validator.useValidatePropTypes;
|
|
12
|
+
exports.throwRequiredError = errorTemplates.throwRequiredError;
|
|
13
|
+
exports.throwTypeError = errorTemplates.throwTypeError;
|
|
14
|
+
exports.useValidateTypescriptPropTypes = typescriptValidator.useValidateTypescriptPropTypes;
|
|
15
|
+
exports.validateTypescriptPropTypesImplementation = typescriptValidator.validateTypescriptPropTypesImplementation;
|
|
@@ -2,38 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
format.split('').forEach(
|
|
5
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
6
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
8
|
+
|
|
9
|
+
const isPrimitiveType = format => ['string', 'number', 'boolean'].includes(format);
|
|
10
|
+
const isUndefined = format => format === '"undefined"';
|
|
11
|
+
const isUnion = format => {
|
|
12
|
+
let depth = 0;
|
|
13
|
+
let satisfies = false;
|
|
14
|
+
format.split('').forEach(char => {
|
|
15
15
|
if (['{', '('].includes(char)) depth += 1;else if (['}', ')'].includes(char)) depth -= 1;else if (char === '|' && depth === 0) satisfies = true;
|
|
16
16
|
});
|
|
17
17
|
return satisfies;
|
|
18
18
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
var isObject = function isObject(format) {
|
|
26
|
-
return format === 'object' || !isUnion(format) && format[0] === '{' && format.slice(-1) === '}';
|
|
27
|
-
};
|
|
28
|
-
var isFunction = function isFunction(format) {
|
|
29
|
-
return !isUnion(format) && format === '((...args: any[]) => any)';
|
|
30
|
-
};
|
|
31
|
-
var isJSXorNode = function isJSXorNode(format) {
|
|
32
|
-
return !isUnion(format) && ['React.ReactNode', 'JSX.Element'].includes(format);
|
|
33
|
-
};
|
|
34
|
-
var isSomethingWithParenthesis = function isSomethingWithParenthesis(format) {
|
|
35
|
-
return !isUnion(format) && format[0] === '(' && format.slice(-1) === ')';
|
|
36
|
-
};
|
|
19
|
+
const isString = format => !isUnion(format) && format[0] === '"' && format.slice(-1) === '"';
|
|
20
|
+
const isArray = format => !isUnion(format) && format.slice(-2) === '[]';
|
|
21
|
+
const isObject = format => format === 'object' || !isUnion(format) && format[0] === '{' && format.slice(-1) === '}';
|
|
22
|
+
const isFunction = format => !isUnion(format) && format === '((...args: any[]) => any)';
|
|
23
|
+
const isJSXorNode = format => !isUnion(format) && ['React.ReactNode', 'JSX.Element'].includes(format);
|
|
24
|
+
const isSomethingWithParenthesis = format => !isUnion(format) && format[0] === '(' && format.slice(-1) === ')';
|
|
37
25
|
|
|
38
26
|
exports.isArray = isArray;
|
|
39
27
|
exports.isFunction = isFunction;
|
|
@@ -44,4 +32,3 @@ exports.isSomethingWithParenthesis = isSomethingWithParenthesis;
|
|
|
44
32
|
exports.isString = isString;
|
|
45
33
|
exports.isUndefined = isUndefined;
|
|
46
34
|
exports.isUnion = isUnion;
|
|
47
|
-
//# sourceMappingURL=typescriptGuards.js.map
|
|
@@ -2,15 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
6
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
8
|
+
|
|
5
9
|
/* eslint-disable complexity */
|
|
6
|
-
|
|
7
|
-
|
|
10
|
+
const typescriptObjectParser = format => {
|
|
11
|
+
const keyValuePairs = []; // State of the algorithm
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
let lastKey = '';
|
|
14
|
+
let lastValue = '';
|
|
15
|
+
let shouldAppendToKey = true;
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
const pushPair = () => {
|
|
14
18
|
if (lastKey) keyValuePairs.push([lastKey, lastValue]);
|
|
15
19
|
lastKey = '';
|
|
16
20
|
lastValue = '';
|
|
@@ -18,8 +22,8 @@ var typescriptObjectParser = function typescriptObjectParser(format) {
|
|
|
18
22
|
}; // Complex -- but working -- logic
|
|
19
23
|
|
|
20
24
|
|
|
21
|
-
|
|
22
|
-
format.split('').forEach(
|
|
25
|
+
let depth = 0;
|
|
26
|
+
format.split('').forEach(char => {
|
|
23
27
|
if (char === '{') {
|
|
24
28
|
depth += 1;
|
|
25
29
|
if (depth > 1) lastValue += char;
|
|
@@ -39,4 +43,3 @@ var typescriptObjectParser = function typescriptObjectParser(format) {
|
|
|
39
43
|
};
|
|
40
44
|
|
|
41
45
|
exports.typescriptObjectParser = typescriptObjectParser;
|
|
42
|
-
//# sourceMappingURL=typescriptParsers.js.map
|
|
@@ -2,17 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
6
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
8
|
+
require('core-js/modules/web.dom-collections.iterator.js');
|
|
9
|
+
var reactDesc = require('react-desc');
|
|
7
10
|
var react = require('react');
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
-
|
|
14
|
-
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
|
|
15
|
-
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
|
|
11
|
+
var errorTemplates = require('./errorTemplates.js');
|
|
12
|
+
var typescriptGuards = require('./typescriptGuards.js');
|
|
13
|
+
var typescriptParsers = require('./typescriptParsers.js');
|
|
16
14
|
|
|
17
15
|
// =============================================================================
|
|
18
16
|
// Atom Validators
|
|
@@ -20,56 +18,53 @@ var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
|
|
|
20
18
|
// This functions will validate something from the data
|
|
21
19
|
// and optionally recursively apply `validateValueWithFormat`
|
|
22
20
|
// in smaller parts
|
|
23
|
-
|
|
21
|
+
const validateUndefined = (schemaName, key, value, format) => {
|
|
24
22
|
if (value !== undefined) {
|
|
25
|
-
|
|
23
|
+
errorTemplates.throwTypeError(schemaName, key, value, format);
|
|
26
24
|
}
|
|
27
25
|
};
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
27
|
+
const validatePrimitiveType = (schemaName, key, value, format) => {
|
|
28
|
+
if (typeof value !== format) {
|
|
29
|
+
errorTemplates.throwTypeError(schemaName, key, value, format);
|
|
32
30
|
}
|
|
33
31
|
};
|
|
34
32
|
|
|
35
|
-
|
|
33
|
+
const validateString = (schemaName, key, value, format) => {
|
|
36
34
|
if (value !== format.slice(1, -1)) {
|
|
37
|
-
|
|
35
|
+
errorTemplates.throwTypeError(schemaName, key, value, format);
|
|
38
36
|
}
|
|
39
37
|
};
|
|
40
38
|
|
|
41
|
-
|
|
39
|
+
const validateArray = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
|
|
42
40
|
// Check that we have an array
|
|
43
41
|
if (!Array.isArray(value)) {
|
|
44
|
-
|
|
42
|
+
errorTemplates.throwTypeError(schemaName, key, value, format);
|
|
45
43
|
} // Check that each element inside satisfies the format
|
|
46
44
|
|
|
47
45
|
|
|
48
|
-
value.forEach(
|
|
46
|
+
value.forEach((val, index) => {
|
|
49
47
|
validateValueWithFormat(schemaName, "".concat(key, "[").concat(index, "]"), val, format.slice(0, -2), validationsMemo, nextValidationsMemo);
|
|
50
48
|
});
|
|
51
49
|
};
|
|
52
50
|
|
|
53
|
-
|
|
51
|
+
const validateObject = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
|
|
54
52
|
// Check that we have an object
|
|
55
|
-
if (
|
|
56
|
-
|
|
53
|
+
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
54
|
+
errorTemplates.throwTypeError(schemaName, key, value, format);
|
|
57
55
|
}
|
|
58
56
|
|
|
59
57
|
if (format === 'object') return;
|
|
60
|
-
|
|
58
|
+
const keyValuePairs = typescriptParsers.typescriptObjectParser(format); // Now we have the key - value pairs
|
|
61
59
|
// Each key could either be required or not
|
|
62
60
|
// Just recursively check the object
|
|
63
61
|
|
|
64
|
-
keyValuePairs.forEach(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
objectValue = _ref2[1];
|
|
68
|
-
|
|
69
|
-
var trueKey = objectKey.slice(-1) === '?' ? objectKey.slice(0, -1) : objectKey;
|
|
62
|
+
keyValuePairs.forEach(_ref => {
|
|
63
|
+
let [objectKey, objectValue] = _ref;
|
|
64
|
+
const trueKey = objectKey.slice(-1) === '?' ? objectKey.slice(0, -1) : objectKey;
|
|
70
65
|
|
|
71
66
|
if (trueKey === objectKey && !(trueKey in value)) {
|
|
72
|
-
|
|
67
|
+
errorTemplates.throwRequiredError(schemaName, key);
|
|
73
68
|
}
|
|
74
69
|
|
|
75
70
|
if (trueKey in value) {
|
|
@@ -78,10 +73,10 @@ var validateObject = function validateObject(schemaName, key, value, format, val
|
|
|
78
73
|
});
|
|
79
74
|
};
|
|
80
75
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
possibilities.forEach(
|
|
76
|
+
const validateUnion = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
|
|
77
|
+
const possibilities = format.split(' | ');
|
|
78
|
+
const errors = [];
|
|
79
|
+
possibilities.forEach(possibility => {
|
|
85
80
|
try {
|
|
86
81
|
validateValueWithFormat(schemaName, key, value, possibility, validationsMemo, nextValidationsMemo);
|
|
87
82
|
} catch (e) {
|
|
@@ -90,27 +85,27 @@ var validateUnion = function validateUnion(schemaName, key, value, format, valid
|
|
|
90
85
|
});
|
|
91
86
|
|
|
92
87
|
if (errors.length === possibilities.length) {
|
|
93
|
-
|
|
88
|
+
errorTemplates.throwTypeError(schemaName, key, value, format);
|
|
94
89
|
}
|
|
95
90
|
};
|
|
96
91
|
|
|
97
|
-
|
|
92
|
+
const validateFunction = (schemaName, key, value, format) => {
|
|
98
93
|
// Check that we have a function
|
|
99
94
|
if (typeof value !== 'function') {
|
|
100
|
-
|
|
95
|
+
errorTemplates.throwTypeError(schemaName, key, value, format);
|
|
101
96
|
}
|
|
102
97
|
};
|
|
103
98
|
|
|
104
|
-
|
|
105
|
-
if (format === 'JSX.Element' && (
|
|
106
|
-
|
|
99
|
+
const validateJSXorNode = (schemaName, key, value, format) => {
|
|
100
|
+
if (format === 'JSX.Element' && (typeof value !== 'object' || !('$$typeof' in value))) {
|
|
101
|
+
errorTemplates.throwTypeError(schemaName, key, value, format);
|
|
107
102
|
}
|
|
108
103
|
}; // =============================================================================
|
|
109
104
|
// Schema validator
|
|
110
105
|
// =============================================================================
|
|
111
106
|
|
|
112
107
|
|
|
113
|
-
|
|
108
|
+
const validateValueWithFormat = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
|
|
114
109
|
nextValidationsMemo[value] = format;
|
|
115
110
|
|
|
116
111
|
if (value in validationsMemo) {
|
|
@@ -118,59 +113,69 @@ var validateValueWithFormat = function validateValueWithFormat(schemaName, key,
|
|
|
118
113
|
return;
|
|
119
114
|
}
|
|
120
115
|
|
|
121
|
-
if (
|
|
116
|
+
if (typescriptGuards.isUndefined(format)) {
|
|
122
117
|
validateUndefined(schemaName, key, value, format);
|
|
123
|
-
} else if (
|
|
118
|
+
} else if (typescriptGuards.isPrimitiveType(format)) {
|
|
124
119
|
validatePrimitiveType(schemaName, key, value, format);
|
|
125
|
-
} else if (
|
|
120
|
+
} else if (typescriptGuards.isUnion(format)) {
|
|
126
121
|
validateUnion(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
127
|
-
} else if (
|
|
122
|
+
} else if (typescriptGuards.isString(format)) {
|
|
128
123
|
validateString(schemaName, key, value, format);
|
|
129
|
-
} else if (
|
|
124
|
+
} else if (typescriptGuards.isArray(format)) {
|
|
130
125
|
validateArray(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
131
|
-
} else if (
|
|
126
|
+
} else if (typescriptGuards.isObject(format)) {
|
|
132
127
|
validateObject(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
|
|
133
|
-
} else if (
|
|
128
|
+
} else if (typescriptGuards.isFunction(format)) {
|
|
134
129
|
validateFunction(schemaName, key, value, format);
|
|
135
|
-
} else if (
|
|
130
|
+
} else if (typescriptGuards.isJSXorNode(format)) {
|
|
136
131
|
validateJSXorNode(schemaName, key, value, format);
|
|
137
|
-
} else if (
|
|
132
|
+
} else if (typescriptGuards.isSomethingWithParenthesis(format)) {
|
|
138
133
|
validateValueWithFormat(schemaName, key, value, format.slice(1, -1), validationsMemo, nextValidationsMemo);
|
|
139
134
|
}
|
|
140
135
|
};
|
|
141
136
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
137
|
+
const validateTypescriptPropTypesImplementation = function (props, schema) {
|
|
138
|
+
let validationsMemo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
139
|
+
let nextValidationsMemo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
140
|
+
const {
|
|
141
|
+
properties,
|
|
142
|
+
name: schemaName
|
|
143
|
+
} = schema;
|
|
144
|
+
properties.forEach(property => {
|
|
145
|
+
const {
|
|
146
|
+
name,
|
|
147
|
+
format,
|
|
148
|
+
required
|
|
149
|
+
} = property;
|
|
151
150
|
|
|
152
151
|
if (required && !(name in props)) {
|
|
153
|
-
|
|
152
|
+
errorTemplates.throwRequiredError(schema.name, name);
|
|
154
153
|
}
|
|
155
154
|
|
|
156
155
|
if (name in props && (props[name] !== undefined || required)) {
|
|
157
156
|
validateValueWithFormat(schemaName, name, props[name], format, validationsMemo, nextValidationsMemo);
|
|
158
157
|
}
|
|
159
158
|
});
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
159
|
+
}; // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
160
|
+
|
|
161
|
+
const useValidateTypescriptPropTypes = (props, propTypes) => {
|
|
162
|
+
const [validationsMemo, setValidationsMemo] = react.useState({}); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
163
|
+
|
|
164
|
+
const ComponentWithSchema = react.useMemo(() => {
|
|
165
|
+
const Component = () => {}; // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
return reactDesc.describe(Component);
|
|
169
|
+
}, []); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
|
|
170
|
+
|
|
171
|
+
ComponentWithSchema.propTypes = propTypes;
|
|
172
|
+
react.useMemo(() => {
|
|
173
|
+
const nextValidationsMemo = {};
|
|
174
|
+
validateTypescriptPropTypesImplementation(props, // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
|
175
|
+
ComponentWithSchema.toTypescript(), validationsMemo, nextValidationsMemo);
|
|
170
176
|
setValidationsMemo(nextValidationsMemo); // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
171
177
|
}, [props]);
|
|
172
178
|
};
|
|
173
179
|
|
|
174
180
|
exports.useValidateTypescriptPropTypes = useValidateTypescriptPropTypes;
|
|
175
181
|
exports.validateTypescriptPropTypesImplementation = validateTypescriptPropTypesImplementation;
|
|
176
|
-
//# sourceMappingURL=typescriptValidator.js.map
|
|
@@ -2,27 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
6
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
8
|
+
var errorTemplates = require('./errorTemplates.js');
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
const useValidatePropTypes = (props, schema) => {
|
|
11
|
+
schema.properties.forEach(property => {
|
|
12
|
+
const propertyName = property.name;
|
|
13
|
+
const currentProp = props[propertyName];
|
|
14
|
+
const currentPropTypeOf = typeof currentProp; // eslint-disable-next-line max-len
|
|
9
15
|
|
|
10
|
-
|
|
16
|
+
const currentFormat = property.format; // this is csv representing types e.g.: "string"/"string,number"/"[object],string"
|
|
11
17
|
|
|
12
|
-
|
|
13
|
-
schema.properties.forEach(function (property) {
|
|
14
|
-
var propertyName = property.name;
|
|
15
|
-
var currentProp = props[propertyName];
|
|
16
|
-
|
|
17
|
-
var currentPropTypeOf = _typeof__default['default'](currentProp); // eslint-disable-next-line max-len
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var currentFormat = property.format; // this is csv representing types e.g.: "string"/"string,number"/"[object],string"
|
|
21
|
-
|
|
22
|
-
var isValidReactElement = false; // this depends on react desc definition
|
|
18
|
+
let isValidReactElement = false; // this depends on react desc definition
|
|
23
19
|
|
|
24
20
|
if (property.required && !Object.prototype.hasOwnProperty.call(props, property.name)) {
|
|
25
|
-
|
|
21
|
+
errorTemplates.throwRequiredError(schema.name, property.name);
|
|
26
22
|
}
|
|
27
23
|
|
|
28
24
|
if (currentPropTypeOf !== 'undefined' && currentProp !== null) {
|
|
@@ -31,11 +27,10 @@ var useValidatePropTypes = function useValidatePropTypes(props, schema) {
|
|
|
31
27
|
}
|
|
32
28
|
|
|
33
29
|
if (!currentFormat.includes(currentPropTypeOf) && !currentFormat.includes(currentProp) && !isValidReactElement) {
|
|
34
|
-
|
|
30
|
+
errorTemplates.throwTypeError(schema.name, propertyName, currentProp, currentFormat);
|
|
35
31
|
}
|
|
36
32
|
}
|
|
37
33
|
});
|
|
38
34
|
};
|
|
39
35
|
|
|
40
36
|
exports.useValidatePropTypes = useValidatePropTypes;
|
|
41
|
-
//# sourceMappingURL=validator.js.map
|