@sprucelabs/schema 29.2.0 → 29.2.2

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.
@@ -56,6 +56,9 @@ class SpruceError extends error_1.default {
56
56
  message = `${map[options.code]}:\n\n${this.renderParametersWithFriendlyMessages(options.parameters, options.friendlyMessages)}`;
57
57
  break;
58
58
  }
59
+ case 'FIELDS_NOT_MAPPED':
60
+ message = `The following fields were not mapped because they don't exist in your map: ${options.fields.join(', ')}`;
61
+ break;
59
62
  default:
60
63
  message = this.message;
61
64
  }
@@ -1,6 +1,6 @@
1
1
  import AbstractSpruceError, { ErrorOptions as ISpruceErrorOptions } from '@sprucelabs/error';
2
2
  import { FieldType } from '../fields/field.static.types';
3
- export type SchemaErrorOptions = DuplicateSchemaErrorOptions | SchemaErrorOptionsNotFound | TransformationFailedErrorOptions | InvalidSchemaDefinitionErrorOptions | NotImplementedErrorOptions | InvalidFieldRegistrationErrorOptions | VersionRequiredErrorOptions | MissingParametersOptions | InvalidParametersOptions | UnexpectedParametersOptions | ValidationFailedErrorOptions | InvalidSchemaReferenceErrorOptions;
3
+ export type SchemaErrorOptions = DuplicateSchemaErrorOptions | SchemaErrorOptionsNotFound | TransformationFailedErrorOptions | InvalidSchemaDefinitionErrorOptions | NotImplementedErrorOptions | InvalidFieldRegistrationErrorOptions | VersionRequiredErrorOptions | MissingParametersOptions | InvalidParametersOptions | UnexpectedParametersOptions | ValidationFailedErrorOptions | InvalidSchemaReferenceErrorOptions | FieldsNotMappedErrorOptions;
4
4
  export type FieldErrorCode = 'MISSING_PARAMETER' | 'INVALID_PARAMETER' | 'UNEXPECTED_PARAMETER';
5
5
  export interface FieldError {
6
6
  code: FieldErrorCode;
@@ -41,6 +41,10 @@ export interface NotImplementedErrorOptions extends ISpruceErrorOptions {
41
41
  code: 'NOT_IMPLEMENTED';
42
42
  instructions: string;
43
43
  }
44
+ export interface FieldsNotMappedErrorOptions extends ISpruceErrorOptions {
45
+ code: 'FIELDS_NOT_MAPPED';
46
+ fields: string[];
47
+ }
44
48
  export interface InvalidFieldRegistrationErrorOptions extends ISpruceErrorOptions {
45
49
  code: 'INVALID_FIELD_REGISTRATION';
46
50
  package: string;
@@ -51,6 +51,9 @@ export default class SpruceError extends AbstractSpruceError {
51
51
  message = `${map[options.code]}:\n\n${this.renderParametersWithFriendlyMessages(options.parameters, options.friendlyMessages)}`;
52
52
  break;
53
53
  }
54
+ case 'FIELDS_NOT_MAPPED':
55
+ message = `The following fields were not mapped because they don't exist in your map: ${options.fields.join(', ')}`;
56
+ break;
54
57
  default:
55
58
  message = this.message;
56
59
  }
@@ -1,6 +1,6 @@
1
1
  import AbstractSpruceError, { ErrorOptions as ISpruceErrorOptions } from '@sprucelabs/error';
2
2
  import { FieldType } from '../fields/field.static.types';
3
- export type SchemaErrorOptions = DuplicateSchemaErrorOptions | SchemaErrorOptionsNotFound | TransformationFailedErrorOptions | InvalidSchemaDefinitionErrorOptions | NotImplementedErrorOptions | InvalidFieldRegistrationErrorOptions | VersionRequiredErrorOptions | MissingParametersOptions | InvalidParametersOptions | UnexpectedParametersOptions | ValidationFailedErrorOptions | InvalidSchemaReferenceErrorOptions;
3
+ export type SchemaErrorOptions = DuplicateSchemaErrorOptions | SchemaErrorOptionsNotFound | TransformationFailedErrorOptions | InvalidSchemaDefinitionErrorOptions | NotImplementedErrorOptions | InvalidFieldRegistrationErrorOptions | VersionRequiredErrorOptions | MissingParametersOptions | InvalidParametersOptions | UnexpectedParametersOptions | ValidationFailedErrorOptions | InvalidSchemaReferenceErrorOptions | FieldsNotMappedErrorOptions;
4
4
  export type FieldErrorCode = 'MISSING_PARAMETER' | 'INVALID_PARAMETER' | 'UNEXPECTED_PARAMETER';
5
5
  export interface FieldError {
6
6
  code: FieldErrorCode;
@@ -41,6 +41,10 @@ export interface NotImplementedErrorOptions extends ISpruceErrorOptions {
41
41
  code: 'NOT_IMPLEMENTED';
42
42
  instructions: string;
43
43
  }
44
+ export interface FieldsNotMappedErrorOptions extends ISpruceErrorOptions {
45
+ code: 'FIELDS_NOT_MAPPED';
46
+ fields: string[];
47
+ }
44
48
  export interface InvalidFieldRegistrationErrorOptions extends ISpruceErrorOptions {
45
49
  code: 'INVALID_FIELD_REGISTRATION';
46
50
  package: string;
@@ -33,13 +33,17 @@ export function validateDateValue(options) {
33
33
  if (typeof value === 'number' || value instanceof Date) {
34
34
  return [];
35
35
  }
36
- else {
37
- return [
38
- {
39
- name,
40
- code: 'INVALID_PARAMETER',
41
- friendlyMessage: `This doesn't look like a date to me!`,
42
- },
43
- ];
36
+ else if (typeof value === 'string') {
37
+ const date = new Date(value);
38
+ if (date.toString() !== 'Invalid Date') {
39
+ return [];
40
+ }
44
41
  }
42
+ return [
43
+ {
44
+ name,
45
+ code: 'INVALID_PARAMETER',
46
+ friendlyMessage: `This doesn't look like a date to me!`,
47
+ },
48
+ ];
45
49
  }
@@ -4,5 +4,8 @@ export default class KeyMapper {
4
4
  mapTo(values: Record<string, any>): any;
5
5
  mapFrom(values: Record<string, any>): any;
6
6
  mapFieldNameTo(name: string): any;
7
- mapFieldNameFrom(name: string): string | null;
7
+ private throwFieldsNotMapped;
8
+ mapFieldNameFrom(name: string): string;
9
+ private _mapTo;
10
+ private _mapFrom;
8
11
  }
@@ -1,16 +1,26 @@
1
+ import SpruceError from '../errors/SpruceError.js';
1
2
  export default class KeyMapper {
2
3
  constructor(map) {
3
4
  this.map = map;
4
5
  }
5
6
  mapTo(values) {
6
- return keyMapper.mapTo(values, this.map);
7
+ return this._mapTo(values, this.map);
7
8
  }
8
9
  mapFrom(values) {
9
- return keyMapper.mapFrom(values, this.map);
10
+ return this._mapFrom(values, this.map);
10
11
  }
11
12
  mapFieldNameTo(name) {
13
+ if (!this.map[name]) {
14
+ this.throwFieldsNotMapped([name]);
15
+ }
12
16
  return this.map[name];
13
17
  }
18
+ throwFieldsNotMapped(fields) {
19
+ throw new SpruceError({
20
+ code: 'FIELDS_NOT_MAPPED',
21
+ fields,
22
+ });
23
+ }
14
24
  mapFieldNameFrom(name) {
15
25
  for (const key in this.map) {
16
26
  // eslint-disable-next-line no-prototype-builtins
@@ -20,29 +30,40 @@ export default class KeyMapper {
20
30
  }
21
31
  }
22
32
  }
23
- return null;
33
+ this.throwFieldsNotMapped([name]);
34
+ return 'never hit';
24
35
  }
25
- }
26
- const keyMapper = {
27
- mapTo(values, map) {
36
+ _mapTo(values, map) {
37
+ const foundFields = [];
28
38
  let target = {};
29
39
  for (const key in map) {
30
40
  // eslint-disable-next-line no-prototype-builtins
31
41
  if (values.hasOwnProperty(key)) {
32
42
  target[map[key]] = values[key];
43
+ foundFields.push(key);
33
44
  }
34
45
  }
46
+ const missingFields = Object.keys(values).filter((key) => !foundFields.includes(key));
47
+ if (missingFields.length > 0) {
48
+ this.throwFieldsNotMapped(missingFields);
49
+ }
35
50
  return target;
36
- },
37
- mapFrom(values, map) {
51
+ }
52
+ _mapFrom(values, map) {
53
+ const foundFields = [];
38
54
  let target = {};
39
55
  for (const targetKey in map) {
40
56
  const sourceKey = map[targetKey];
41
57
  // eslint-disable-next-line no-prototype-builtins
42
58
  if (values.hasOwnProperty(sourceKey)) {
43
59
  target[targetKey] = values[sourceKey];
60
+ foundFields.push(sourceKey);
44
61
  }
45
62
  }
63
+ const missingFields = Object.keys(values).filter((key) => !foundFields.includes(key));
64
+ if (missingFields.length > 0) {
65
+ this.throwFieldsNotMapped(missingFields);
66
+ }
46
67
  return target;
47
- },
48
- };
68
+ }
69
+ }
@@ -40,14 +40,18 @@ function validateDateValue(options) {
40
40
  if (typeof value === 'number' || value instanceof Date) {
41
41
  return [];
42
42
  }
43
- else {
44
- return [
45
- {
46
- name,
47
- code: 'INVALID_PARAMETER',
48
- friendlyMessage: `This doesn't look like a date to me!`,
49
- },
50
- ];
43
+ else if (typeof value === 'string') {
44
+ const date = new Date(value);
45
+ if (date.toString() !== 'Invalid Date') {
46
+ return [];
47
+ }
51
48
  }
49
+ return [
50
+ {
51
+ name,
52
+ code: 'INVALID_PARAMETER',
53
+ friendlyMessage: `This doesn't look like a date to me!`,
54
+ },
55
+ ];
52
56
  }
53
57
  exports.validateDateValue = validateDateValue;
@@ -4,5 +4,8 @@ export default class KeyMapper {
4
4
  mapTo(values: Record<string, any>): any;
5
5
  mapFrom(values: Record<string, any>): any;
6
6
  mapFieldNameTo(name: string): any;
7
- mapFieldNameFrom(name: string): string | null;
7
+ private throwFieldsNotMapped;
8
+ mapFieldNameFrom(name: string): string;
9
+ private _mapTo;
10
+ private _mapFrom;
8
11
  }
@@ -1,18 +1,31 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
3
7
  class KeyMapper {
4
8
  constructor(map) {
5
9
  this.map = map;
6
10
  }
7
11
  mapTo(values) {
8
- return keyMapper.mapTo(values, this.map);
12
+ return this._mapTo(values, this.map);
9
13
  }
10
14
  mapFrom(values) {
11
- return keyMapper.mapFrom(values, this.map);
15
+ return this._mapFrom(values, this.map);
12
16
  }
13
17
  mapFieldNameTo(name) {
18
+ if (!this.map[name]) {
19
+ this.throwFieldsNotMapped([name]);
20
+ }
14
21
  return this.map[name];
15
22
  }
23
+ throwFieldsNotMapped(fields) {
24
+ throw new SpruceError_1.default({
25
+ code: 'FIELDS_NOT_MAPPED',
26
+ fields,
27
+ });
28
+ }
16
29
  mapFieldNameFrom(name) {
17
30
  for (const key in this.map) {
18
31
  // eslint-disable-next-line no-prototype-builtins
@@ -22,30 +35,41 @@ class KeyMapper {
22
35
  }
23
36
  }
24
37
  }
25
- return null;
38
+ this.throwFieldsNotMapped([name]);
39
+ return 'never hit';
26
40
  }
27
- }
28
- exports.default = KeyMapper;
29
- const keyMapper = {
30
- mapTo(values, map) {
41
+ _mapTo(values, map) {
42
+ const foundFields = [];
31
43
  let target = {};
32
44
  for (const key in map) {
33
45
  // eslint-disable-next-line no-prototype-builtins
34
46
  if (values.hasOwnProperty(key)) {
35
47
  target[map[key]] = values[key];
48
+ foundFields.push(key);
36
49
  }
37
50
  }
51
+ const missingFields = Object.keys(values).filter((key) => !foundFields.includes(key));
52
+ if (missingFields.length > 0) {
53
+ this.throwFieldsNotMapped(missingFields);
54
+ }
38
55
  return target;
39
- },
40
- mapFrom(values, map) {
56
+ }
57
+ _mapFrom(values, map) {
58
+ const foundFields = [];
41
59
  let target = {};
42
60
  for (const targetKey in map) {
43
61
  const sourceKey = map[targetKey];
44
62
  // eslint-disable-next-line no-prototype-builtins
45
63
  if (values.hasOwnProperty(sourceKey)) {
46
64
  target[targetKey] = values[sourceKey];
65
+ foundFields.push(sourceKey);
47
66
  }
48
67
  }
68
+ const missingFields = Object.keys(values).filter((key) => !foundFields.includes(key));
69
+ if (missingFields.length > 0) {
70
+ this.throwFieldsNotMapped(missingFields);
71
+ }
49
72
  return target;
50
- },
51
- };
73
+ }
74
+ }
75
+ exports.default = KeyMapper;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "!build/__tests__",
9
9
  "esm"
10
10
  ],
11
- "version": "29.2.0",
11
+ "version": "29.2.2",
12
12
  "main": "./build/index.js",
13
13
  "types": "./build/index.d.ts",
14
14
  "module": "./build/esm/index.js",