@elliemae/ds-props-helpers 2.0.1 → 2.1.0-rc.10

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.
@@ -16,9 +16,9 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
16
16
  var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
17
17
  var deepequal__default = /*#__PURE__*/_interopDefaultLegacy(deepequal);
18
18
 
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; }
19
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
20
20
 
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; }
21
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
22
22
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
23
 
24
24
  const useMemoCompare = (next, compare) => {
@@ -8,6 +8,7 @@ require('core-js/modules/esnext.iterator.for-each.js');
8
8
 
9
9
  const isPrimitiveType = format => ['string', 'number', 'boolean'].includes(format);
10
10
  const isUndefined = format => format === '"undefined"';
11
+ const isNull = format => format === '"null"';
11
12
  const isUnion = format => {
12
13
  let depth = 0;
13
14
  let satisfies = false;
@@ -26,6 +27,7 @@ const isSomethingWithParenthesis = format => !isUnion(format) && format[0] === '
26
27
  exports.isArray = isArray;
27
28
  exports.isFunction = isFunction;
28
29
  exports.isJSXorNode = isJSXorNode;
30
+ exports.isNull = isNull;
29
31
  exports.isObject = isObject;
30
32
  exports.isPrimitiveType = isPrimitiveType;
31
33
  exports.isSomethingWithParenthesis = isSomethingWithParenthesis;
@@ -19,7 +19,13 @@ var typescriptParsers = require('./typescriptParsers.js');
19
19
  // and optionally recursively apply `validateValueWithFormat`
20
20
  // in smaller parts
21
21
  const validateUndefined = (schemaName, key, value, format) => {
22
- if (value !== undefined) {
22
+ if (value !== undefined || value === 'undefined') {
23
+ errorTemplates.throwTypeError(schemaName, key, value, format);
24
+ }
25
+ };
26
+
27
+ const validateNull = (schemaName, key, value, format) => {
28
+ if (value !== null || value === 'null') {
23
29
  errorTemplates.throwTypeError(schemaName, key, value, format);
24
30
  }
25
31
  };
@@ -44,14 +50,22 @@ const validateArray = (schemaName, key, value, format, validationsMemo, nextVali
44
50
 
45
51
 
46
52
  value.forEach((val, index) => {
53
+ // this is a recursive func, we need to invoke it before it's defined.
54
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
47
55
  validateValueWithFormat(schemaName, "".concat(key, "[").concat(index, "]"), val, format.slice(0, -2), validationsMemo, nextValidationsMemo);
48
56
  });
49
57
  };
50
58
 
59
+ function isObjectType(value) {
60
+ return !(typeof value !== 'object' || Array.isArray(value));
61
+ }
62
+
51
63
  const validateObject = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
52
- // Check that we have an object
53
- if (typeof value !== 'object' || Array.isArray(value)) {
64
+ const valuesIsObject = isObjectType(value); // Check that we have an object
65
+
66
+ if (!valuesIsObject) {
54
67
  errorTemplates.throwTypeError(schemaName, key, value, format);
68
+ return;
55
69
  }
56
70
 
57
71
  if (format === 'object') return;
@@ -68,16 +82,20 @@ const validateObject = (schemaName, key, value, format, validationsMemo, nextVal
68
82
  }
69
83
 
70
84
  if (trueKey in value) {
85
+ // this is a recursive func, we need to invoke it before it's defined.
86
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
71
87
  validateValueWithFormat(schemaName, "".concat(key, "[").concat(trueKey, "]"), value[trueKey], objectValue, validationsMemo, nextValidationsMemo);
72
88
  }
73
89
  });
74
90
  };
75
91
 
76
92
  const validateUnion = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
77
- const possibilities = format.split(' | ');
93
+ const possibilities = format.split(/\s?\|\s?/);
78
94
  const errors = [];
79
95
  possibilities.forEach(possibility => {
80
96
  try {
97
+ // this is a recursive func, we need to invoke it before it's defined.
98
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
81
99
  validateValueWithFormat(schemaName, key, value, possibility, validationsMemo, nextValidationsMemo);
82
100
  } catch (e) {
83
101
  errors.push(e);
@@ -96,8 +114,14 @@ const validateFunction = (schemaName, key, value, format) => {
96
114
  }
97
115
  };
98
116
 
117
+ function isJSXElement(value) {
118
+ return value === null || typeof value === 'object' && value !== null && '$$typeof' in value;
119
+ }
120
+
99
121
  const validateJSXorNode = (schemaName, key, value, format) => {
100
- if (format === 'JSX.Element' && (typeof value !== 'object' || !('$$typeof' in value))) {
122
+ const valueIsJSX = isJSXElement(value);
123
+
124
+ if (format === 'JSX.Element' && !valueIsJSX) {
101
125
  errorTemplates.throwTypeError(schemaName, key, value, format);
102
126
  }
103
127
  }; // =============================================================================
@@ -115,6 +139,8 @@ const validateValueWithFormat = (schemaName, key, value, format, validationsMemo
115
139
 
116
140
  if (typescriptGuards.isUndefined(format)) {
117
141
  validateUndefined(schemaName, key, value, format);
142
+ } else if (typescriptGuards.isNull(format)) {
143
+ validateNull(schemaName, key, value, format);
118
144
  } else if (typescriptGuards.isPrimitiveType(format)) {
119
145
  validatePrimitiveType(schemaName, key, value, format);
120
146
  } else if (typescriptGuards.isUnion(format)) {
@@ -7,9 +7,9 @@ import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
7
7
  import { useRef } from 'react';
8
8
  import deepequal from 'fast-deep-equal/react';
9
9
 
10
- 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; }
10
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
11
11
 
12
- 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(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; }
12
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
13
13
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
14
 
15
15
  const useMemoCompare = (next, compare) => {
@@ -4,6 +4,7 @@ import 'core-js/modules/esnext.iterator.for-each.js';
4
4
 
5
5
  const isPrimitiveType = format => ['string', 'number', 'boolean'].includes(format);
6
6
  const isUndefined = format => format === '"undefined"';
7
+ const isNull = format => format === '"null"';
7
8
  const isUnion = format => {
8
9
  let depth = 0;
9
10
  let satisfies = false;
@@ -19,4 +20,4 @@ const isFunction = format => !isUnion(format) && format === '((...args: any[]) =
19
20
  const isJSXorNode = format => !isUnion(format) && ['React.ReactNode', 'JSX.Element'].includes(format);
20
21
  const isSomethingWithParenthesis = format => !isUnion(format) && format[0] === '(' && format.slice(-1) === ')';
21
22
 
22
- export { isArray, isFunction, isJSXorNode, isObject, isPrimitiveType, isSomethingWithParenthesis, isString, isUndefined, isUnion };
23
+ export { isArray, isFunction, isJSXorNode, isNull, isObject, isPrimitiveType, isSomethingWithParenthesis, isString, isUndefined, isUnion };
@@ -5,7 +5,7 @@ import 'core-js/modules/web.dom-collections.iterator.js';
5
5
  import { describe } from 'react-desc';
6
6
  import { useState, useMemo } from 'react';
7
7
  import { throwRequiredError, throwTypeError } from './errorTemplates.js';
8
- import { isUndefined, isPrimitiveType, isUnion, isString, isArray, isObject, isFunction, isJSXorNode, isSomethingWithParenthesis } from './typescriptGuards.js';
8
+ import { isUndefined, isNull, isPrimitiveType, isUnion, isString, isArray, isObject, isFunction, isJSXorNode, isSomethingWithParenthesis } from './typescriptGuards.js';
9
9
  import { typescriptObjectParser } from './typescriptParsers.js';
10
10
 
11
11
  // =============================================================================
@@ -15,7 +15,13 @@ import { typescriptObjectParser } from './typescriptParsers.js';
15
15
  // and optionally recursively apply `validateValueWithFormat`
16
16
  // in smaller parts
17
17
  const validateUndefined = (schemaName, key, value, format) => {
18
- if (value !== undefined) {
18
+ if (value !== undefined || value === 'undefined') {
19
+ throwTypeError(schemaName, key, value, format);
20
+ }
21
+ };
22
+
23
+ const validateNull = (schemaName, key, value, format) => {
24
+ if (value !== null || value === 'null') {
19
25
  throwTypeError(schemaName, key, value, format);
20
26
  }
21
27
  };
@@ -40,14 +46,22 @@ const validateArray = (schemaName, key, value, format, validationsMemo, nextVali
40
46
 
41
47
 
42
48
  value.forEach((val, index) => {
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
43
51
  validateValueWithFormat(schemaName, "".concat(key, "[").concat(index, "]"), val, format.slice(0, -2), validationsMemo, nextValidationsMemo);
44
52
  });
45
53
  };
46
54
 
55
+ function isObjectType(value) {
56
+ return !(typeof value !== 'object' || Array.isArray(value));
57
+ }
58
+
47
59
  const validateObject = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
48
- // Check that we have an object
49
- if (typeof value !== 'object' || Array.isArray(value)) {
60
+ const valuesIsObject = isObjectType(value); // Check that we have an object
61
+
62
+ if (!valuesIsObject) {
50
63
  throwTypeError(schemaName, key, value, format);
64
+ return;
51
65
  }
52
66
 
53
67
  if (format === 'object') return;
@@ -64,16 +78,20 @@ const validateObject = (schemaName, key, value, format, validationsMemo, nextVal
64
78
  }
65
79
 
66
80
  if (trueKey in value) {
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
67
83
  validateValueWithFormat(schemaName, "".concat(key, "[").concat(trueKey, "]"), value[trueKey], objectValue, validationsMemo, nextValidationsMemo);
68
84
  }
69
85
  });
70
86
  };
71
87
 
72
88
  const validateUnion = (schemaName, key, value, format, validationsMemo, nextValidationsMemo) => {
73
- const possibilities = format.split(' | ');
89
+ const possibilities = format.split(/\s?\|\s?/);
74
90
  const errors = [];
75
91
  possibilities.forEach(possibility => {
76
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
77
95
  validateValueWithFormat(schemaName, key, value, possibility, validationsMemo, nextValidationsMemo);
78
96
  } catch (e) {
79
97
  errors.push(e);
@@ -92,8 +110,14 @@ const validateFunction = (schemaName, key, value, format) => {
92
110
  }
93
111
  };
94
112
 
113
+ function isJSXElement(value) {
114
+ return value === null || typeof value === 'object' && value !== null && '$$typeof' in value;
115
+ }
116
+
95
117
  const validateJSXorNode = (schemaName, key, value, format) => {
96
- if (format === 'JSX.Element' && (typeof value !== 'object' || !('$$typeof' in value))) {
118
+ const valueIsJSX = isJSXElement(value);
119
+
120
+ if (format === 'JSX.Element' && !valueIsJSX) {
97
121
  throwTypeError(schemaName, key, value, format);
98
122
  }
99
123
  }; // =============================================================================
@@ -111,6 +135,8 @@ const validateValueWithFormat = (schemaName, key, value, format, validationsMemo
111
135
 
112
136
  if (isUndefined(format)) {
113
137
  validateUndefined(schemaName, key, value, format);
138
+ } else if (isNull(format)) {
139
+ validateNull(schemaName, key, value, format);
114
140
  } else if (isPrimitiveType(format)) {
115
141
  validatePrimitiveType(schemaName, key, value, format);
116
142
  } else if (isUnion(format)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elliemae/ds-props-helpers",
3
- "version": "2.0.1",
3
+ "version": "2.1.0-rc.10",
4
4
  "license": "MIT",
5
5
  "description": "ICE MT - Dimsum - Props Helpers",
6
6
  "module": "./esm/index.js",
@@ -1,11 +1,12 @@
1
- declare type guardFn = (format: string) => boolean;
2
- export declare const isPrimitiveType: guardFn;
3
- export declare const isUndefined: guardFn;
4
- export declare const isUnion: guardFn;
5
- export declare const isString: guardFn;
6
- export declare const isArray: guardFn;
7
- export declare const isObject: guardFn;
8
- export declare const isFunction: guardFn;
9
- export declare const isJSXorNode: guardFn;
10
- export declare const isSomethingWithParenthesis: guardFn;
1
+ declare type GuardFn = (format: string) => boolean;
2
+ export declare const isPrimitiveType: GuardFn;
3
+ export declare const isUndefined: GuardFn;
4
+ export declare const isNull: GuardFn;
5
+ export declare const isUnion: GuardFn;
6
+ export declare const isString: GuardFn;
7
+ export declare const isArray: GuardFn;
8
+ export declare const isObject: GuardFn;
9
+ export declare const isFunction: GuardFn;
10
+ export declare const isJSXorNode: GuardFn;
11
+ export declare const isSomethingWithParenthesis: GuardFn;
11
12
  export {};
@@ -1,4 +1,4 @@
1
- import React, { PropsWithChildren } from 'react';
1
+ import { PropsWithChildren } from 'react';
2
2
  interface TypescriptSchema {
3
3
  description: string;
4
4
  name: string;
@@ -11,5 +11,5 @@ interface TypescriptSchema {
11
11
  }[];
12
12
  }
13
13
  export declare const validateTypescriptPropTypesImplementation: (props: PropsWithChildren<Record<string, unknown>>, schema: TypescriptSchema, validationsMemo?: Record<string, string>, nextValidationsMemo?: Record<string, string>) => void;
14
- export declare const useValidateTypescriptPropTypes: <T = Record<string, any>>(props: React.PropsWithChildren<T>, propTypes: any) => void;
14
+ export declare const useValidateTypescriptPropTypes: <T = Record<string, any>>(props: PropsWithChildren<T>, propTypes: any) => void;
15
15
  export {};