@sprucelabs/schema 28.6.49 → 28.7.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.
@@ -158,9 +158,12 @@ class StaticSchemaEntityImplementation extends AbstractEntity_1.default {
158
158
  }
159
159
  getValues(options) {
160
160
  const values = {};
161
- const { fields = Object.keys(this.fields), shouldIncludePrivateFields: includePrivateFields = true, } = options || {};
161
+ const { fields = Object.keys(this.fields), shouldIncludePrivateFields: includePrivateFields = true, shouldOnlyIncludeFieldsWithValues, } = options || {};
162
162
  this.getNamedFields().forEach((namedField) => {
163
163
  const { name, field } = namedField;
164
+ if (shouldOnlyIncludeFieldsWithValues && !(name in this.values)) {
165
+ return;
166
+ }
164
167
  if (fields.indexOf(name) > -1 &&
165
168
  (includePrivateFields || !field.isPrivate)) {
166
169
  const value = this.get(name, options);
@@ -130,9 +130,12 @@ class StaticSchemaEntityImplementation extends AbstractEntity {
130
130
  }
131
131
  getValues(options) {
132
132
  const values = {};
133
- const { fields = Object.keys(this.fields), shouldIncludePrivateFields: includePrivateFields = true, } = options || {};
133
+ const { fields = Object.keys(this.fields), shouldIncludePrivateFields: includePrivateFields = true, shouldOnlyIncludeFieldsWithValues, } = options || {};
134
134
  this.getNamedFields().forEach((namedField) => {
135
135
  const { name, field } = namedField;
136
+ if (shouldOnlyIncludeFieldsWithValues && !(name in this.values)) {
137
+ return;
138
+ }
136
139
  if (fields.indexOf(name) > -1 &&
137
140
  (includePrivateFields || !field.isPrivate)) {
138
141
  const value = this.get(name, options);
@@ -121,6 +121,7 @@ export interface SchemaNormalizeOptions<S extends Schema, CreateEntityInstances
121
121
  byField?: {
122
122
  [K in SchemaFieldNames<S>]?: S['fields'][K] extends FieldDefinition ? Partial<FieldDefinitionMap[S['fields'][K]['type']]['options']> : never;
123
123
  };
124
+ shouldOnlyIncludeFieldsWithValues?: boolean;
124
125
  }
125
126
  export interface DynamicSchemaNormalizeOptions<CreateEntityInstances extends boolean> extends SchemaNormalizeFieldValueOptions<CreateEntityInstances> {
126
127
  }
@@ -0,0 +1,2 @@
1
+ import { Schema, SchemaPartialValues, SchemaGetValuesOptions, SchemaFieldNames, SchemaPublicFieldNames, SchemaValues } from '../schemas.static.types';
2
+ export default function normalizePartialSchemaValues<S extends Schema, F extends SchemaFieldNames<S> = SchemaFieldNames<S>, PF extends SchemaPublicFieldNames<S> = SchemaPublicFieldNames<S>, CreateEntityInstances extends boolean = false, IncludePrivateFields extends boolean = true, Values extends SchemaPartialValues<S, CreateEntityInstances> = SchemaPartialValues<S, CreateEntityInstances>, Fields extends keyof Values = keyof Values>(schema: S, values: Values, options?: SchemaGetValuesOptions<S, F, PF, CreateEntityInstances, IncludePrivateFields>): Required<Pick<SchemaValues<S, CreateEntityInstances>, Fields>>;
@@ -0,0 +1,5 @@
1
+ import normalizeSchemaValues from './normalizeSchemaValues.js';
2
+ export default function normalizePartialSchemaValues(schema, values, options) {
3
+ const normalized = normalizeSchemaValues(schema, values, Object.assign(Object.assign({}, options), { shouldOnlyIncludeFieldsWithValues: true }));
4
+ return normalized;
5
+ }
@@ -12,7 +12,7 @@ var __rest = (this && this.__rest) || function (s, e) {
12
12
  import EntityFactory from '../factories/SchemaEntityFactory.js';
13
13
  export default function normalizeSchemaValues(schema, values, options) {
14
14
  const instance = EntityFactory.Entity(schema, values);
15
- const _a = options || {}, { shouldCreateEntityInstances: createEntityInstances = false } = _a, rest = __rest(_a, ["shouldCreateEntityInstances"]);
16
- const normalizedOptions = Object.assign({ shouldCreateEntityInstances: createEntityInstances }, rest);
15
+ const _a = options || {}, { shouldCreateEntityInstances = false } = _a, rest = __rest(_a, ["shouldCreateEntityInstances"]);
16
+ const normalizedOptions = Object.assign({ shouldCreateEntityInstances }, rest);
17
17
  return instance.getValues(normalizedOptions);
18
18
  }
@@ -121,6 +121,7 @@ export interface SchemaNormalizeOptions<S extends Schema, CreateEntityInstances
121
121
  byField?: {
122
122
  [K in SchemaFieldNames<S>]?: S['fields'][K] extends FieldDefinition ? Partial<FieldDefinitionMap[S['fields'][K]['type']]['options']> : never;
123
123
  };
124
+ shouldOnlyIncludeFieldsWithValues?: boolean;
124
125
  }
125
126
  export interface DynamicSchemaNormalizeOptions<CreateEntityInstances extends boolean> extends SchemaNormalizeFieldValueOptions<CreateEntityInstances> {
126
127
  }
@@ -0,0 +1,2 @@
1
+ import { Schema, SchemaPartialValues, SchemaGetValuesOptions, SchemaFieldNames, SchemaPublicFieldNames, SchemaValues } from '../schemas.static.types';
2
+ export default function normalizePartialSchemaValues<S extends Schema, F extends SchemaFieldNames<S> = SchemaFieldNames<S>, PF extends SchemaPublicFieldNames<S> = SchemaPublicFieldNames<S>, CreateEntityInstances extends boolean = false, IncludePrivateFields extends boolean = true, Values extends SchemaPartialValues<S, CreateEntityInstances> = SchemaPartialValues<S, CreateEntityInstances>, Fields extends keyof Values = keyof Values>(schema: S, values: Values, options?: SchemaGetValuesOptions<S, F, PF, CreateEntityInstances, IncludePrivateFields>): Required<Pick<SchemaValues<S, CreateEntityInstances>, Fields>>;
@@ -0,0 +1,11 @@
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 normalizeSchemaValues_1 = __importDefault(require("./normalizeSchemaValues"));
7
+ function normalizePartialSchemaValues(schema, values, options) {
8
+ const normalized = (0, normalizeSchemaValues_1.default)(schema, values, Object.assign(Object.assign({}, options), { shouldOnlyIncludeFieldsWithValues: true }));
9
+ return normalized;
10
+ }
11
+ exports.default = normalizePartialSchemaValues;
@@ -17,8 +17,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  const SchemaEntityFactory_1 = __importDefault(require("../factories/SchemaEntityFactory"));
18
18
  function normalizeSchemaValues(schema, values, options) {
19
19
  const instance = SchemaEntityFactory_1.default.Entity(schema, values);
20
- const _a = options || {}, { shouldCreateEntityInstances: createEntityInstances = false } = _a, rest = __rest(_a, ["shouldCreateEntityInstances"]);
21
- const normalizedOptions = Object.assign({ shouldCreateEntityInstances: createEntityInstances }, rest);
20
+ const _a = options || {}, { shouldCreateEntityInstances = false } = _a, rest = __rest(_a, ["shouldCreateEntityInstances"]);
21
+ const normalizedOptions = Object.assign({ shouldCreateEntityInstances }, rest);
22
22
  return instance.getValues(normalizedOptions);
23
23
  }
24
24
  exports.default = normalizeSchemaValues;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "!build/__tests__",
9
9
  "esm"
10
10
  ],
11
- "version": "28.6.49",
11
+ "version": "28.7.0",
12
12
  "main": "./build/index.js",
13
13
  "types": "./build/index.d.ts",
14
14
  "module": "./build/esm/index.js",