@elliemae/ds-props-helpers 2.2.0-alpha.5 → 2.2.0-next.3

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 (59) hide show
  1. package/cjs/defaultProps/index.js +9 -28
  2. package/cjs/defaultProps/useMemoMergePropsWithDefault.js +44 -44
  3. package/cjs/getProps/index.js +20 -37
  4. package/cjs/globalProps/constants.js +15 -0
  5. package/cjs/globalProps/globalAttributesPropTypes.js +372 -0
  6. package/cjs/globalProps/index.js +11 -0
  7. package/cjs/globalProps/useGetGlobalAttributes.js +36 -0
  8. package/cjs/index.js +24 -30
  9. package/cjs/validation/errorTemplates.js +12 -44
  10. package/cjs/validation/index.js +15 -30
  11. package/cjs/validation/typescriptGuards.js +31 -60
  12. package/cjs/validation/typescriptParsers.js +34 -65
  13. package/cjs/validation/typescriptValidator.js +135 -99
  14. package/cjs/validation/validator.js +24 -43
  15. package/esm/defaultProps/index.js +1 -3
  16. package/esm/defaultProps/useMemoMergePropsWithDefault.js +34 -14
  17. package/esm/getProps/index.js +15 -8
  18. package/esm/globalProps/constants.js +11 -0
  19. package/esm/globalProps/globalAttributesPropTypes.js +368 -0
  20. package/esm/globalProps/index.js +2 -0
  21. package/esm/globalProps/useGetGlobalAttributes.js +32 -0
  22. package/esm/index.js +7 -5
  23. package/esm/validation/errorTemplates.js +7 -15
  24. package/esm/validation/index.js +3 -5
  25. package/esm/validation/typescriptGuards.js +18 -31
  26. package/esm/validation/typescriptParsers.js +30 -36
  27. package/esm/validation/typescriptValidator.js +106 -57
  28. package/esm/validation/validator.js +18 -12
  29. package/package.json +17 -1
  30. package/types/globalProps/constants.d.ts +3 -0
  31. package/types/globalProps/globalAttributesPropTypes.d.ts +2169 -0
  32. package/types/globalProps/index.d.ts +2 -0
  33. package/types/globalProps/useGetGlobalAttributes.d.ts +5 -0
  34. package/types/index.d.ts +1 -0
  35. package/types/validation/typescriptValidator.d.ts +2 -2
  36. package/cjs/defaultProps/index.js.map +0 -7
  37. package/cjs/defaultProps/useMemoMergePropsWithDefault.js.map +0 -7
  38. package/cjs/getProps/index.js.map +0 -7
  39. package/cjs/index.js.map +0 -7
  40. package/cjs/tests/test.schema.js +0 -67
  41. package/cjs/tests/test.schema.js.map +0 -7
  42. package/cjs/validation/errorTemplates.js.map +0 -7
  43. package/cjs/validation/index.js.map +0 -7
  44. package/cjs/validation/typescriptGuards.js.map +0 -7
  45. package/cjs/validation/typescriptParsers.js.map +0 -7
  46. package/cjs/validation/typescriptValidator.js.map +0 -7
  47. package/cjs/validation/validator.js.map +0 -7
  48. package/esm/defaultProps/index.js.map +0 -7
  49. package/esm/defaultProps/useMemoMergePropsWithDefault.js.map +0 -7
  50. package/esm/getProps/index.js.map +0 -7
  51. package/esm/index.js.map +0 -7
  52. package/esm/tests/test.schema.js +0 -38
  53. package/esm/tests/test.schema.js.map +0 -7
  54. package/esm/validation/errorTemplates.js.map +0 -7
  55. package/esm/validation/index.js.map +0 -7
  56. package/esm/validation/typescriptGuards.js.map +0 -7
  57. package/esm/validation/typescriptParsers.js.map +0 -7
  58. package/esm/validation/typescriptValidator.js.map +0 -7
  59. package/esm/validation/validator.js.map +0 -7
@@ -1,153 +1,202 @@
1
- import * as React from "react";
2
- import { describe } from "react-desc";
3
- import { useMemo, useState } from "react";
4
- import { throwRequiredError, throwTypeError } from "./errorTemplates";
5
- import {
6
- isArray,
7
- isFunction,
8
- isJSXorNode,
9
- isObject,
10
- isPrimitiveType,
11
- isSomethingWithParenthesis,
12
- isString,
13
- isUnion,
14
- isUndefined,
15
- isNull
16
- } from "./typescriptGuards";
17
- import { typescriptObjectParser } from "./typescriptParsers";
1
+ import 'core-js/modules/esnext.async-iterator.for-each.js';
2
+ import 'core-js/modules/esnext.iterator.constructor.js';
3
+ import 'core-js/modules/esnext.iterator.for-each.js';
4
+ import 'core-js/modules/web.dom-collections.iterator.js';
5
+ import { describe } from 'react-desc';
6
+ import { useState, useMemo } from 'react';
7
+ import { throwRequiredError, throwTypeError } from './errorTemplates.js';
8
+ import { isUndefined, isNull, isPrimitiveType, isUnion, isString, isArray, isObject, isFunction, isJSXorNode, isSomethingWithParenthesis } from './typescriptGuards.js';
9
+ import { typescriptObjectParser } from './typescriptParsers.js';
10
+
11
+ // =============================================================================
12
+ // Atom Validators
13
+ // =============================================================================
14
+ // This functions will validate something from the data
15
+ // and optionally recursively apply `validateValueWithFormat`
16
+ // in smaller parts
18
17
  const validateUndefined = (schemaName, key, value, format) => {
19
- if (value !== void 0 || value === "undefined") {
18
+ if (value !== undefined || value === 'undefined') {
20
19
  throwTypeError(schemaName, key, value, format);
21
20
  }
22
21
  };
22
+
23
23
  const validateNull = (schemaName, key, value, format) => {
24
- if (value !== null || value === "null") {
24
+ if (value !== null || value === 'null') {
25
25
  throwTypeError(schemaName, key, value, format);
26
26
  }
27
27
  };
28
+
28
29
  const validatePrimitiveType = (schemaName, key, value, format) => {
29
30
  if (typeof value !== format) {
30
31
  throwTypeError(schemaName, key, value, format);
31
32
  }
32
33
  };
34
+
33
35
  const validateString = (schemaName, key, value, format) => {
34
36
  if (value !== format.slice(1, -1)) {
35
37
  throwTypeError(schemaName, key, value, format);
36
38
  }
37
39
  };
40
+
38
41
  const validateArray = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
42
+ // Check that we have an array
39
43
  if (!Array.isArray(value)) {
40
44
  throwTypeError(schemaName, key, value, format);
41
- }
45
+ } // Check that each element inside satisfies the format
46
+
47
+
42
48
  value.forEach((val, index) => {
43
- validateValueWithFormat(schemaName, `${key}[${index}]`, val, format.slice(0, -2), validationsMemo, nextValidationsMemo);
49
+ // this is a recursive func, we need to invoke it before it's defined.
50
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
51
+ validateValueWithFormat(schemaName, "".concat(key, "[").concat(index, "]"), val, format.slice(0, -2), validationsMemo, nextValidationsMemo);
44
52
  });
45
53
  };
54
+
46
55
  function isObjectType(value) {
47
- return !(typeof value !== "object" || Array.isArray(value));
56
+ return !(typeof value !== 'object' || Array.isArray(value));
48
57
  }
58
+
49
59
  const validateObject = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
50
- const valuesIsObject = isObjectType(value);
60
+ const valuesIsObject = isObjectType(value); // Check that we have an object
61
+
51
62
  if (!valuesIsObject) {
52
63
  throwTypeError(schemaName, key, value, format);
53
64
  return;
54
65
  }
55
- if (format === "object")
56
- return;
57
- const keyValuePairs = typescriptObjectParser(format);
58
- keyValuePairs.forEach(([objectKey, objectValue]) => {
59
- const trueKey = objectKey.slice(-1) === "?" ? objectKey.slice(0, -1) : objectKey;
66
+
67
+ if (format === 'object') return;
68
+ const keyValuePairs = typescriptObjectParser(format); // Now we have the key - value pairs
69
+ // Each key could either be required or not
70
+ // Just recursively check the object
71
+
72
+ keyValuePairs.forEach(_ref => {
73
+ let [objectKey, objectValue] = _ref;
74
+ const trueKey = objectKey.slice(-1) === '?' ? objectKey.slice(0, -1) : objectKey;
75
+
60
76
  if (trueKey === objectKey && !(trueKey in value)) {
61
77
  throwRequiredError(schemaName, key);
62
78
  }
79
+
63
80
  if (trueKey in value) {
64
- validateValueWithFormat(schemaName, `${key}[${trueKey}]`, value[trueKey], objectValue, validationsMemo, nextValidationsMemo);
81
+ // this is a recursive func, we need to invoke it before it's defined.
82
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
83
+ validateValueWithFormat(schemaName, "".concat(key, "[").concat(trueKey, "]"), value[trueKey], objectValue, validationsMemo, nextValidationsMemo);
65
84
  }
66
85
  });
67
86
  };
87
+
68
88
  const validateUnion = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
69
89
  const possibilities = format.split(/\s?\|\s?/);
70
90
  const errors = [];
71
- possibilities.forEach((possibility) => {
91
+ possibilities.forEach(possibility => {
72
92
  try {
93
+ // this is a recursive func, we need to invoke it before it's defined.
94
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
73
95
  validateValueWithFormat(schemaName, key, value, possibility, validationsMemo, nextValidationsMemo);
74
96
  } catch (e) {
75
97
  errors.push(e);
76
98
  }
77
99
  });
100
+
78
101
  if (errors.length === possibilities.length) {
79
102
  throwTypeError(schemaName, key, value, format);
80
103
  }
81
104
  };
105
+
82
106
  const validateFunction = (schemaName, key, value, format) => {
83
- if (typeof value !== "function") {
107
+ // Check that we have a function
108
+ if (typeof value !== 'function') {
84
109
  throwTypeError(schemaName, key, value, format);
85
110
  }
86
111
  };
112
+
87
113
  function isJSXElement(value) {
88
- return value === null || typeof value === "object" && value !== null && "$$typeof" in value;
114
+ return value === null || typeof value === 'object' && value !== null && '$$typeof' in value;
89
115
  }
116
+
90
117
  const validateJSXorNode = (schemaName, key, value, format) => {
91
118
  const valueIsJSX = isJSXElement(value);
92
- if (format === "JSX.Element" && !valueIsJSX) {
119
+
120
+ if (format === 'JSX.Element' && !valueIsJSX) {
93
121
  throwTypeError(schemaName, key, value, format);
94
122
  }
95
- };
123
+ }; // =============================================================================
124
+ // Schema validator
125
+ // =============================================================================
126
+
127
+
96
128
  const validateValueWithFormat = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
97
129
  nextValidationsMemo[value] = format;
130
+
98
131
  if (value in validationsMemo) {
132
+ // We already validated this value on this format
99
133
  return;
100
134
  }
135
+
101
136
  if (isUndefined(format)) {
102
- validateUndefined(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
137
+ validateUndefined(schemaName, key, value, format);
103
138
  } else if (isNull(format)) {
104
- validateNull(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
139
+ validateNull(schemaName, key, value, format);
105
140
  } else if (isPrimitiveType(format)) {
106
- validatePrimitiveType(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
141
+ validatePrimitiveType(schemaName, key, value, format);
107
142
  } else if (isUnion(format)) {
108
143
  validateUnion(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
109
144
  } else if (isString(format)) {
110
- validateString(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
145
+ validateString(schemaName, key, value, format);
111
146
  } else if (isArray(format)) {
112
147
  validateArray(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
113
148
  } else if (isObject(format)) {
114
149
  validateObject(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
115
150
  } else if (isFunction(format)) {
116
- validateFunction(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
151
+ validateFunction(schemaName, key, value, format);
117
152
  } else if (isJSXorNode(format)) {
118
- validateJSXorNode(schemaName, key, value, format, validationsMemo, nextValidationsMemo);
153
+ validateJSXorNode(schemaName, key, value, format);
119
154
  } else if (isSomethingWithParenthesis(format)) {
120
155
  validateValueWithFormat(schemaName, key, value, format.slice(1, -1), validationsMemo, nextValidationsMemo);
121
156
  }
122
157
  };
123
- const validateTypescriptPropTypesImplementation = (props, schema, validationsMemo = {}, nextValidationsMemo = {}) => {
124
- const { properties, name: schemaName } = schema;
125
- properties.forEach((property) => {
126
- const { name, format, required } = property;
158
+
159
+ const validateTypescriptPropTypesImplementation = function (props, schema) {
160
+ let validationsMemo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
161
+ let nextValidationsMemo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
162
+ const {
163
+ properties,
164
+ name: schemaName
165
+ } = schema;
166
+ properties.forEach(property => {
167
+ const {
168
+ name,
169
+ format,
170
+ required
171
+ } = property;
172
+
127
173
  if (required && !(name in props)) {
128
174
  throwRequiredError(schema.name, name);
129
175
  }
130
- if (name in props && (props[name] !== void 0 || required)) {
176
+
177
+ if (name in props && (props[name] !== undefined || required)) {
131
178
  validateValueWithFormat(schemaName, name, props[name], format, validationsMemo, nextValidationsMemo);
132
179
  }
133
180
  });
134
- };
181
+ }; // eslint-disable-next-line @typescript-eslint/no-explicit-any
182
+
135
183
  const useValidateTypescriptPropTypes = (props, propTypes) => {
136
- const [validationsMemo, setValidationsMemo] = useState({});
184
+ const [validationsMemo, setValidationsMemo] = useState({}); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
185
+
137
186
  const ComponentWithSchema = useMemo(() => {
138
- const Component = () => {
139
- };
187
+ const Component = () => {}; // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
188
+
189
+
140
190
  return describe(Component);
141
- }, []);
191
+ }, []); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
192
+
142
193
  ComponentWithSchema.propTypes = propTypes;
143
194
  useMemo(() => {
144
195
  const nextValidationsMemo = {};
145
- validateTypescriptPropTypesImplementation(props, ComponentWithSchema.toTypescript(), validationsMemo, nextValidationsMemo);
146
- setValidationsMemo(nextValidationsMemo);
196
+ validateTypescriptPropTypesImplementation(props, // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
197
+ ComponentWithSchema.toTypescript(), validationsMemo, nextValidationsMemo);
198
+ setValidationsMemo(nextValidationsMemo); // eslint-disable-next-line react-hooks/exhaustive-deps
147
199
  }, [props]);
148
200
  };
149
- export {
150
- useValidateTypescriptPropTypes,
151
- validateTypescriptPropTypesImplementation
152
- };
153
- //# sourceMappingURL=typescriptValidator.js.map
201
+
202
+ export { useValidateTypescriptPropTypes, validateTypescriptPropTypesImplementation };
@@ -1,26 +1,32 @@
1
- import * as React from "react";
2
- import { throwRequiredError, throwTypeError } from "./errorTemplates";
1
+ import 'core-js/modules/esnext.async-iterator.for-each.js';
2
+ import 'core-js/modules/esnext.iterator.constructor.js';
3
+ import 'core-js/modules/esnext.iterator.for-each.js';
4
+ import { throwRequiredError, throwTypeError } from './errorTemplates.js';
5
+
3
6
  const useValidatePropTypes = (props, schema) => {
4
- schema.properties.forEach((property) => {
7
+ schema.properties.forEach(property => {
5
8
  const propertyName = property.name;
6
9
  const currentProp = props[propertyName];
7
- const currentPropTypeOf = typeof currentProp;
8
- const currentFormat = property.format;
9
- let isValidReactElement = false;
10
+ const currentPropTypeOf = typeof currentProp; // eslint-disable-next-line max-len
11
+
12
+ const currentFormat = property.format; // this is csv representing types e.g.: "string"/"string,number"/"[object],string"
13
+
14
+ let isValidReactElement = false; // this depends on react desc definition
15
+
10
16
  if (property.required && !Object.prototype.hasOwnProperty.call(props, property.name)) {
11
17
  throwRequiredError(schema.name, property.name);
12
18
  }
13
- if (currentPropTypeOf !== "undefined" && currentProp !== null) {
14
- if (currentPropTypeOf === "object" && Object.prototype.hasOwnProperty.call(currentProp, "$$typeof") && (currentFormat.includes("node") || currentFormat.includes("element"))) {
19
+
20
+ if (currentPropTypeOf !== 'undefined' && currentProp !== null) {
21
+ if (currentPropTypeOf === 'object' && Object.prototype.hasOwnProperty.call(currentProp, '$$typeof') && (currentFormat.includes('node') || currentFormat.includes('element'))) {
15
22
  isValidReactElement = true;
16
23
  }
24
+
17
25
  if (!currentFormat.includes(currentPropTypeOf) && !currentFormat.includes(currentProp) && !isValidReactElement) {
18
26
  throwTypeError(schema.name, propertyName, currentProp, currentFormat);
19
27
  }
20
28
  }
21
29
  });
22
30
  };
23
- export {
24
- useValidatePropTypes
25
- };
26
- //# sourceMappingURL=validator.js.map
31
+
32
+ export { useValidatePropTypes };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elliemae/ds-props-helpers",
3
- "version": "2.2.0-alpha.5",
3
+ "version": "2.2.0-next.3",
4
4
  "license": "MIT",
5
5
  "description": "ICE MT - Dimsum - Props Helpers",
6
6
  "module": "./esm/index.js",
@@ -39,6 +39,22 @@
39
39
  "import": "./esm/tests/test.schema.js",
40
40
  "require": "./cjs/tests/test.schema.js"
41
41
  },
42
+ "./globalProps/useGetGlobalAttributes": {
43
+ "import": "./esm/globalProps/useGetGlobalAttributes.js",
44
+ "require": "./cjs/globalProps/useGetGlobalAttributes.js"
45
+ },
46
+ "./globalProps": {
47
+ "import": "./esm/globalProps/index.js",
48
+ "require": "./cjs/globalProps/index.js"
49
+ },
50
+ "./globalProps/globalAttributesPropTypes": {
51
+ "import": "./esm/globalProps/globalAttributesPropTypes.js",
52
+ "require": "./cjs/globalProps/globalAttributesPropTypes.js"
53
+ },
54
+ "./globalProps/constants": {
55
+ "import": "./esm/globalProps/constants.js",
56
+ "require": "./cjs/globalProps/constants.js"
57
+ },
42
58
  "./getProps": {
43
59
  "import": "./esm/getProps/index.js",
44
60
  "require": "./cjs/getProps/index.js"
@@ -0,0 +1,3 @@
1
+ import { AriaAttributes, DOMAttributes, HTMLAttributes } from 'react';
2
+ export declare type GlobalAttributes<T = true> = Record<keyof (AriaAttributes & DOMAttributes<Element> & HTMLAttributes<Element>), T>;
3
+ export declare const globalAttributes: GlobalAttributes<true>;