@sprucelabs/schema 28.3.18 → 28.4.0

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.
@@ -29,6 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const AbstractEntity_1 = __importDefault(require("./AbstractEntity"));
30
30
  const SpruceError_1 = __importDefault(require("./errors/SpruceError"));
31
31
  const FieldFactory_1 = __importDefault(require("./factories/FieldFactory"));
32
+ const cloneDeepPreservingInstances_1 = __importDefault(require("./utilities/cloneDeepPreservingInstances"));
32
33
  const normalizeFieldValue_1 = __importStar(require("./utilities/normalizeFieldValue"));
33
34
  /** Universal schema class */
34
35
  class StaticSchemaEntityImplementation extends AbstractEntity_1.default {
@@ -38,7 +39,8 @@ class StaticSchemaEntityImplementation extends AbstractEntity_1.default {
38
39
  this.schema = schema;
39
40
  this.fields = {};
40
41
  this.buildFields();
41
- this.values = Object.assign(Object.assign({}, this.values), values);
42
+ const v = Object.assign(Object.assign({}, this.values), values);
43
+ this.values = (0, cloneDeepPreservingInstances_1.default)(v);
42
44
  }
43
45
  buildFields() {
44
46
  const fieldDefinitions = this.schema.fields;
@@ -1,6 +1,7 @@
1
1
  import AbstractEntity from './AbstractEntity.js';
2
2
  import SpruceError from './errors/SpruceError.js';
3
3
  import FieldFactory from './factories/FieldFactory.js';
4
+ import cloneDeepPreservingInstances from './utilities/cloneDeepPreservingInstances.js';
4
5
  import normalizeFieldValue, { normalizeValueToArray, } from './utilities/normalizeFieldValue.js';
5
6
  /** Universal schema class */
6
7
  export default class StaticSchemaEntityImplementation extends AbstractEntity {
@@ -10,7 +11,8 @@ export default class StaticSchemaEntityImplementation extends AbstractEntity {
10
11
  this.schema = schema;
11
12
  this.fields = {};
12
13
  this.buildFields();
13
- this.values = Object.assign(Object.assign({}, this.values), values);
14
+ const v = Object.assign(Object.assign({}, this.values), values);
15
+ this.values = cloneDeepPreservingInstances(v);
14
16
  }
15
17
  buildFields() {
16
18
  const fieldDefinitions = this.schema.fields;
@@ -4,6 +4,8 @@ import { FieldRegistration } from './utilities/registerFieldType';
4
4
  export default StaticSchemaEntityImplementation;
5
5
  export { default as validationErrorAssert } from './tests/validationErrorAssert.utility';
6
6
  export { default as selectAssert } from './tests/selectAssert.utility';
7
+ export { default as cloneDeep } from './utilities/cloneDeep';
8
+ export { default as cloneDeepPreservingInstances } from './utilities/cloneDeepPreservingInstances';
7
9
  export { default as selectAssertUtil } from './tests/selectAssert.deprecated';
8
10
  export * from './schemas.static.types';
9
11
  export * from './fields';
@@ -19,6 +19,8 @@ import StaticSchemaEntityImplementation from './StaticSchemaEntityImplementation
19
19
  export default StaticSchemaEntityImplementation;
20
20
  export { default as validationErrorAssert } from './tests/validationErrorAssert.utility.js';
21
21
  export { default as selectAssert } from './tests/selectAssert.utility.js';
22
+ export { default as cloneDeep } from './utilities/cloneDeep.js';
23
+ export { default as cloneDeepPreservingInstances } from './utilities/cloneDeepPreservingInstances.js';
22
24
  export { default as selectAssertUtil } from './tests/selectAssert.deprecated.js';
23
25
  export * from './schemas.static.types.js';
24
26
  export * from './fields/index.js';
@@ -0,0 +1,3 @@
1
+ export default function cloneDeep<T>(obj: T, transformer?: Transformer): T;
2
+ declare type Transformer = (value: any, key: string) => any | void;
3
+ export {};
@@ -0,0 +1,46 @@
1
+ export default function cloneDeep(obj, transformer) {
2
+ var _a;
3
+ const o = obj;
4
+ let result = o;
5
+ let type = {}.toString.call(o).slice(8, -1);
6
+ if (type == 'Set') {
7
+ return new Set([...o].map((value) => cloneDeep(value, transformer)));
8
+ }
9
+ if (type == 'Map') {
10
+ const items = [];
11
+ [...o.entries()].forEach((kv) => {
12
+ if ((transformer === null || transformer === void 0 ? void 0 : transformer(kv[1], kv[0])) !== false) {
13
+ items.push([cloneDeep(kv[0]), cloneDeep(kv[1])]);
14
+ }
15
+ });
16
+ return new Map(items);
17
+ }
18
+ if (type == 'Date') {
19
+ return new Date(o.getTime());
20
+ }
21
+ if (type == 'RegExp') {
22
+ return RegExp(o.source, getRegExpFlags(o));
23
+ }
24
+ if (type == 'Array' || type == 'Object') {
25
+ result = Array.isArray(o) ? [] : {};
26
+ for (let key in o) {
27
+ result[key] = (_a = transformer === null || transformer === void 0 ? void 0 : transformer(o[key], key)) !== null && _a !== void 0 ? _a : cloneDeep(o[key], transformer);
28
+ }
29
+ }
30
+ // primitives and non-supported objects (e.g. functions) land here
31
+ return result;
32
+ }
33
+ function getRegExpFlags(regExp) {
34
+ if (typeof regExp.source.flags == 'string') {
35
+ return regExp.source.flags;
36
+ }
37
+ else {
38
+ let flags = [];
39
+ regExp.global && flags.push('g');
40
+ regExp.ignoreCase && flags.push('i');
41
+ regExp.multiline && flags.push('m');
42
+ regExp.sticky && flags.push('y');
43
+ regExp.unicode && flags.push('u');
44
+ return flags.join('');
45
+ }
46
+ }
@@ -0,0 +1 @@
1
+ export default function cloneDeepPreservingInstances<T>(v: T): T;
@@ -0,0 +1,10 @@
1
+ import cloneDeep from './cloneDeep.js';
2
+ export default function cloneDeepPreservingInstances(v) {
3
+ return cloneDeep(v, (value) => {
4
+ var _a, _b;
5
+ const name = (_b = (_a = value === null || value === void 0 ? void 0 : value.__proto__) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name;
6
+ if (name && name !== 'Object') {
7
+ return value;
8
+ }
9
+ });
10
+ }
@@ -9,6 +9,7 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
+ import set from 'just-safe-set';
12
13
  import { validateSchema } from '../index.js';
13
14
  import EntityFactory from '../factories/SchemaEntityFactory.js';
14
15
  export default function validateSchemaValues(schema, values, options
@@ -16,6 +17,11 @@ export default function validateSchemaValues(schema, values, options
16
17
  ) {
17
18
  const opts = __rest(options !== null && options !== void 0 ? options : {}, []);
18
19
  validateSchema(schema);
19
- const instance = EntityFactory.Entity(schema, values);
20
+ const mapped = Object.keys(values).reduce((mapped, key) => {
21
+ //@ts-ignore
22
+ set(mapped, key, values[key]);
23
+ return mapped;
24
+ }, {});
25
+ const instance = EntityFactory.Entity(schema, mapped);
20
26
  instance.validate(opts);
21
27
  }
package/build/index.d.ts CHANGED
@@ -4,6 +4,8 @@ import { FieldRegistration } from './utilities/registerFieldType';
4
4
  export default StaticSchemaEntityImplementation;
5
5
  export { default as validationErrorAssert } from './tests/validationErrorAssert.utility';
6
6
  export { default as selectAssert } from './tests/selectAssert.utility';
7
+ export { default as cloneDeep } from './utilities/cloneDeep';
8
+ export { default as cloneDeepPreservingInstances } from './utilities/cloneDeepPreservingInstances';
7
9
  export { default as selectAssertUtil } from './tests/selectAssert.deprecated';
8
10
  export * from './schemas.static.types';
9
11
  export * from './fields';
package/build/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.mapFieldErrorsToParameterErrors = exports.assertOptions = exports.fieldRegistrations = exports.SchemaRegistry = exports.SchemaEntityFactory = exports.FieldFactory = exports.normalizeSchemaToIdWithVersion = exports.isIdWithVersion = exports.pickFields = exports.getFields = exports.formatPhoneNumber = exports.areSchemasTheSame = exports.isSchemaValid = exports.validateSchema = exports.dropPrivateFields = exports.dropFields = exports.makeFieldsOptional = exports.areSchemaValuesValid = exports.normalizeSchemaValues = exports.validateSchemaValues = exports.defaultSchemaValues = exports.registerFieldType = exports.buildSchema = exports.buildErrorSchema = exports.SchemaError = exports.selectAssertUtil = exports.selectAssert = exports.validationErrorAssert = void 0;
20
+ exports.mapFieldErrorsToParameterErrors = exports.assertOptions = exports.fieldRegistrations = exports.SchemaRegistry = exports.SchemaEntityFactory = exports.FieldFactory = exports.normalizeSchemaToIdWithVersion = exports.isIdWithVersion = exports.pickFields = exports.getFields = exports.formatPhoneNumber = exports.areSchemasTheSame = exports.isSchemaValid = exports.validateSchema = exports.dropPrivateFields = exports.dropFields = exports.makeFieldsOptional = exports.areSchemaValuesValid = exports.normalizeSchemaValues = exports.validateSchemaValues = exports.defaultSchemaValues = exports.registerFieldType = exports.buildSchema = exports.buildErrorSchema = exports.SchemaError = exports.selectAssertUtil = exports.cloneDeepPreservingInstances = exports.cloneDeep = exports.selectAssert = exports.validationErrorAssert = void 0;
21
21
  __exportStar(require("./StaticSchemaEntityImplementation"), exports);
22
22
  const addressField_addon_1 = __importDefault(require("./addons/addressField.addon"));
23
23
  const booleanField_addon_1 = __importDefault(require("./addons/booleanField.addon"));
@@ -41,6 +41,10 @@ var validationErrorAssert_utility_1 = require("./tests/validationErrorAssert.uti
41
41
  Object.defineProperty(exports, "validationErrorAssert", { enumerable: true, get: function () { return __importDefault(validationErrorAssert_utility_1).default; } });
42
42
  var selectAssert_utility_1 = require("./tests/selectAssert.utility");
43
43
  Object.defineProperty(exports, "selectAssert", { enumerable: true, get: function () { return __importDefault(selectAssert_utility_1).default; } });
44
+ var cloneDeep_1 = require("./utilities/cloneDeep");
45
+ Object.defineProperty(exports, "cloneDeep", { enumerable: true, get: function () { return __importDefault(cloneDeep_1).default; } });
46
+ var cloneDeepPreservingInstances_1 = require("./utilities/cloneDeepPreservingInstances");
47
+ Object.defineProperty(exports, "cloneDeepPreservingInstances", { enumerable: true, get: function () { return __importDefault(cloneDeepPreservingInstances_1).default; } });
44
48
  var selectAssert_deprecated_1 = require("./tests/selectAssert.deprecated");
45
49
  Object.defineProperty(exports, "selectAssertUtil", { enumerable: true, get: function () { return __importDefault(selectAssert_deprecated_1).default; } });
46
50
  __exportStar(require("./schemas.static.types"), exports);
@@ -0,0 +1,3 @@
1
+ export default function cloneDeep<T>(obj: T, transformer?: Transformer): T;
2
+ declare type Transformer = (value: any, key: string) => any | void;
3
+ export {};
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function cloneDeep(obj, transformer) {
4
+ var _a;
5
+ const o = obj;
6
+ let result = o;
7
+ let type = {}.toString.call(o).slice(8, -1);
8
+ if (type == 'Set') {
9
+ return new Set([...o].map((value) => cloneDeep(value, transformer)));
10
+ }
11
+ if (type == 'Map') {
12
+ const items = [];
13
+ [...o.entries()].forEach((kv) => {
14
+ if ((transformer === null || transformer === void 0 ? void 0 : transformer(kv[1], kv[0])) !== false) {
15
+ items.push([cloneDeep(kv[0]), cloneDeep(kv[1])]);
16
+ }
17
+ });
18
+ return new Map(items);
19
+ }
20
+ if (type == 'Date') {
21
+ return new Date(o.getTime());
22
+ }
23
+ if (type == 'RegExp') {
24
+ return RegExp(o.source, getRegExpFlags(o));
25
+ }
26
+ if (type == 'Array' || type == 'Object') {
27
+ result = Array.isArray(o) ? [] : {};
28
+ for (let key in o) {
29
+ result[key] = (_a = transformer === null || transformer === void 0 ? void 0 : transformer(o[key], key)) !== null && _a !== void 0 ? _a : cloneDeep(o[key], transformer);
30
+ }
31
+ }
32
+ // primitives and non-supported objects (e.g. functions) land here
33
+ return result;
34
+ }
35
+ exports.default = cloneDeep;
36
+ function getRegExpFlags(regExp) {
37
+ if (typeof regExp.source.flags == 'string') {
38
+ return regExp.source.flags;
39
+ }
40
+ else {
41
+ let flags = [];
42
+ regExp.global && flags.push('g');
43
+ regExp.ignoreCase && flags.push('i');
44
+ regExp.multiline && flags.push('m');
45
+ regExp.sticky && flags.push('y');
46
+ regExp.unicode && flags.push('u');
47
+ return flags.join('');
48
+ }
49
+ }
@@ -0,0 +1 @@
1
+ export default function cloneDeepPreservingInstances<T>(v: T): T;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const cloneDeep_1 = __importDefault(require("./cloneDeep"));
7
+ function cloneDeepPreservingInstances(v) {
8
+ return (0, cloneDeep_1.default)(v, (value) => {
9
+ var _a, _b;
10
+ const name = (_b = (_a = value === null || value === void 0 ? void 0 : value.__proto__) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name;
11
+ if (name && name !== 'Object') {
12
+ return value;
13
+ }
14
+ });
15
+ }
16
+ exports.default = cloneDeepPreservingInstances;
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ const just_safe_set_1 = __importDefault(require("just-safe-set"));
17
18
  const __1 = require("..");
18
19
  const SchemaEntityFactory_1 = __importDefault(require("../factories/SchemaEntityFactory"));
19
20
  function validateSchemaValues(schema, values, options
@@ -21,7 +22,12 @@ function validateSchemaValues(schema, values, options
21
22
  ) {
22
23
  const opts = __rest(options !== null && options !== void 0 ? options : {}, []);
23
24
  (0, __1.validateSchema)(schema);
24
- const instance = SchemaEntityFactory_1.default.Entity(schema, values);
25
+ const mapped = Object.keys(values).reduce((mapped, key) => {
26
+ //@ts-ignore
27
+ (0, just_safe_set_1.default)(mapped, key, values[key]);
28
+ return mapped;
29
+ }, {});
30
+ const instance = SchemaEntityFactory_1.default.Entity(schema, mapped);
25
31
  instance.validate(opts);
26
32
  }
27
33
  exports.default = validateSchemaValues;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "!build/__tests__",
9
9
  "esm"
10
10
  ],
11
- "version": "28.3.18",
11
+ "version": "28.4.0",
12
12
  "main": "./build/index.js",
13
13
  "types": "./build/index.d.ts",
14
14
  "module": "./build/esm/index.js",
@@ -70,7 +70,7 @@
70
70
  "@sprucelabs/test": "^7.7.339",
71
71
  "email-validator": "^2.0.4",
72
72
  "just-safe-get": "^4.0.1",
73
- "lodash": "^4.17.21"
73
+ "just-safe-set": "^4.0.2"
74
74
  },
75
75
  "devDependencies": {
76
76
  "@sprucelabs/esm-postbuild": "^1.0.424",
@@ -78,8 +78,7 @@
78
78
  "@sprucelabs/jest-sheets-reporter": "^1.3.29",
79
79
  "@sprucelabs/resolve-path-aliases": "^1.1.14",
80
80
  "@sprucelabs/semantic-release": "^4.0.8",
81
- "@sprucelabs/test-utils": "^3.1.39",
82
- "@types/lodash": "^4.14.182",
81
+ "@sprucelabs/test-utils": "^3.1.41",
83
82
  "chokidar-cli": "^3.0.0",
84
83
  "concurrently": "^7.3.0",
85
84
  "eslint": "^8.20.0",