@sprucelabs/schema 29.1.2 → 29.2.1
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.
- package/build/errors/SpruceError.js +3 -0
- package/build/errors/options.types.d.ts +5 -1
- package/build/esm/errors/SpruceError.js +3 -0
- package/build/esm/errors/options.types.d.ts +5 -1
- package/build/esm/utilities/KeyMapper.d.ts +5 -0
- package/build/esm/utilities/KeyMapper.js +44 -9
- package/build/utilities/KeyMapper.d.ts +5 -0
- package/build/utilities/KeyMapper.js +48 -10
- package/package.json +1 -1
|
@@ -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;
|
|
@@ -3,4 +3,9 @@ export default class KeyMapper {
|
|
|
3
3
|
constructor(map: Record<string, any>);
|
|
4
4
|
mapTo(values: Record<string, any>): any;
|
|
5
5
|
mapFrom(values: Record<string, any>): any;
|
|
6
|
+
mapFieldNameTo(name: string): any;
|
|
7
|
+
private throwFieldsNotMapped;
|
|
8
|
+
mapFieldNameFrom(name: string): string;
|
|
9
|
+
private _mapTo;
|
|
10
|
+
private _mapFrom;
|
|
6
11
|
}
|
|
@@ -1,34 +1,69 @@
|
|
|
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
|
|
7
|
+
return this._mapTo(values, this.map);
|
|
7
8
|
}
|
|
8
9
|
mapFrom(values) {
|
|
9
|
-
return
|
|
10
|
+
return this._mapFrom(values, this.map);
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
mapFieldNameTo(name) {
|
|
13
|
+
if (!this.map[name]) {
|
|
14
|
+
this.throwFieldsNotMapped([name]);
|
|
15
|
+
}
|
|
16
|
+
return this.map[name];
|
|
17
|
+
}
|
|
18
|
+
throwFieldsNotMapped(fields) {
|
|
19
|
+
throw new SpruceError({
|
|
20
|
+
code: 'FIELDS_NOT_MAPPED',
|
|
21
|
+
fields,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
mapFieldNameFrom(name) {
|
|
25
|
+
for (const key in this.map) {
|
|
26
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
27
|
+
if (this.map.hasOwnProperty(key)) {
|
|
28
|
+
if (this.map[key] === name) {
|
|
29
|
+
return key;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
this.throwFieldsNotMapped([name]);
|
|
34
|
+
return 'never hit';
|
|
35
|
+
}
|
|
36
|
+
_mapTo(values, map) {
|
|
37
|
+
const foundFields = [];
|
|
14
38
|
let target = {};
|
|
15
39
|
for (const key in map) {
|
|
16
40
|
// eslint-disable-next-line no-prototype-builtins
|
|
17
41
|
if (values.hasOwnProperty(key)) {
|
|
18
42
|
target[map[key]] = values[key];
|
|
43
|
+
foundFields.push(key);
|
|
19
44
|
}
|
|
20
45
|
}
|
|
46
|
+
const missingFields = Object.keys(values).filter((key) => !foundFields.includes(key));
|
|
47
|
+
if (missingFields.length > 0) {
|
|
48
|
+
this.throwFieldsNotMapped(missingFields);
|
|
49
|
+
}
|
|
21
50
|
return target;
|
|
22
|
-
}
|
|
23
|
-
|
|
51
|
+
}
|
|
52
|
+
_mapFrom(values, map) {
|
|
53
|
+
const foundFields = [];
|
|
24
54
|
let target = {};
|
|
25
55
|
for (const targetKey in map) {
|
|
26
56
|
const sourceKey = map[targetKey];
|
|
27
57
|
// eslint-disable-next-line no-prototype-builtins
|
|
28
58
|
if (values.hasOwnProperty(sourceKey)) {
|
|
29
59
|
target[targetKey] = values[sourceKey];
|
|
60
|
+
foundFields.push(sourceKey);
|
|
30
61
|
}
|
|
31
62
|
}
|
|
63
|
+
const missingFields = Object.keys(values).filter((key) => !foundFields.includes(key));
|
|
64
|
+
if (missingFields.length > 0) {
|
|
65
|
+
this.throwFieldsNotMapped(missingFields);
|
|
66
|
+
}
|
|
32
67
|
return target;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -3,4 +3,9 @@ export default class KeyMapper {
|
|
|
3
3
|
constructor(map: Record<string, any>);
|
|
4
4
|
mapTo(values: Record<string, any>): any;
|
|
5
5
|
mapFrom(values: Record<string, any>): any;
|
|
6
|
+
mapFieldNameTo(name: string): any;
|
|
7
|
+
private throwFieldsNotMapped;
|
|
8
|
+
mapFieldNameFrom(name: string): string;
|
|
9
|
+
private _mapTo;
|
|
10
|
+
private _mapFrom;
|
|
6
11
|
}
|
|
@@ -1,37 +1,75 @@
|
|
|
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
|
|
12
|
+
return this._mapTo(values, this.map);
|
|
9
13
|
}
|
|
10
14
|
mapFrom(values) {
|
|
11
|
-
return
|
|
15
|
+
return this._mapFrom(values, this.map);
|
|
12
16
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
mapFieldNameTo(name) {
|
|
18
|
+
if (!this.map[name]) {
|
|
19
|
+
this.throwFieldsNotMapped([name]);
|
|
20
|
+
}
|
|
21
|
+
return this.map[name];
|
|
22
|
+
}
|
|
23
|
+
throwFieldsNotMapped(fields) {
|
|
24
|
+
throw new SpruceError_1.default({
|
|
25
|
+
code: 'FIELDS_NOT_MAPPED',
|
|
26
|
+
fields,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
mapFieldNameFrom(name) {
|
|
30
|
+
for (const key in this.map) {
|
|
31
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
32
|
+
if (this.map.hasOwnProperty(key)) {
|
|
33
|
+
if (this.map[key] === name) {
|
|
34
|
+
return key;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
this.throwFieldsNotMapped([name]);
|
|
39
|
+
return 'never hit';
|
|
40
|
+
}
|
|
41
|
+
_mapTo(values, map) {
|
|
42
|
+
const foundFields = [];
|
|
17
43
|
let target = {};
|
|
18
44
|
for (const key in map) {
|
|
19
45
|
// eslint-disable-next-line no-prototype-builtins
|
|
20
46
|
if (values.hasOwnProperty(key)) {
|
|
21
47
|
target[map[key]] = values[key];
|
|
48
|
+
foundFields.push(key);
|
|
22
49
|
}
|
|
23
50
|
}
|
|
51
|
+
const missingFields = Object.keys(values).filter((key) => !foundFields.includes(key));
|
|
52
|
+
if (missingFields.length > 0) {
|
|
53
|
+
this.throwFieldsNotMapped(missingFields);
|
|
54
|
+
}
|
|
24
55
|
return target;
|
|
25
|
-
}
|
|
26
|
-
|
|
56
|
+
}
|
|
57
|
+
_mapFrom(values, map) {
|
|
58
|
+
const foundFields = [];
|
|
27
59
|
let target = {};
|
|
28
60
|
for (const targetKey in map) {
|
|
29
61
|
const sourceKey = map[targetKey];
|
|
30
62
|
// eslint-disable-next-line no-prototype-builtins
|
|
31
63
|
if (values.hasOwnProperty(sourceKey)) {
|
|
32
64
|
target[targetKey] = values[sourceKey];
|
|
65
|
+
foundFields.push(sourceKey);
|
|
33
66
|
}
|
|
34
67
|
}
|
|
68
|
+
const missingFields = Object.keys(values).filter((key) => !foundFields.includes(key));
|
|
69
|
+
if (missingFields.length > 0) {
|
|
70
|
+
this.throwFieldsNotMapped(missingFields);
|
|
71
|
+
}
|
|
35
72
|
return target;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.default = KeyMapper;
|