@xapp/dynamo-service
Advanced tools
Comparing version 1.0.0-beta.1 to 1.0.0-beta.2
@@ -18,3 +18,3 @@ "use strict"; | ||
schemaBuilder: isoSchemaBuilder, | ||
makeObjectTests: () => { | ||
convertToSchemaTests: () => { | ||
it("Tests that the object is converted to ISO format by default.", () => { | ||
@@ -65,3 +65,3 @@ const date = new Date(); | ||
}, | ||
makeUpdateObjectTests: () => { | ||
convertUpdateToSchemaTests: () => { | ||
it("Tests that the object being set to undefined is converted if a processor returns a default.", () => { | ||
@@ -92,3 +92,3 @@ const date = new Date(2018, 1, 1); | ||
schemaBuilder: timestampSchemaBuilder, | ||
makeObjectTests: () => { | ||
convertToSchemaTests: () => { | ||
it("Tests that the object is converted to Timestamp format when explicitly told to.", () => { | ||
@@ -112,3 +112,3 @@ const date = new Date(); | ||
}, | ||
makeUpdateObjectTests: () => { | ||
convertUpdateToSchemaTests: () => { | ||
it("Tests that the object being set to undefined is converted if a processor returns a default.", () => { | ||
@@ -115,0 +115,0 @@ const date = new Date(2018, 1, 1); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Chai = require("chai"); | ||
const ValidatorTestUtils_1 = require("../../__test__/ValidatorTestUtils"); | ||
const NormalSchemaBuilder_test_1 = require("../../Normal/__test__/NormalSchemaBuilder.test"); | ||
const MapSchemaBuilder_1 = require("../MapSchemaBuilder"); | ||
const expect = Chai.expect; | ||
function mapSchemaBuilder(key, schema) { | ||
@@ -13,2 +15,340 @@ return new MapSchemaBuilder_1.default(key, Object.assign({}, schema, { type: "M" })); | ||
schemaBuilder: mapSchemaBuilder, | ||
convertFromSchemaTests: () => { | ||
it("Processes nested objects.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestNumber": { | ||
type: "N", | ||
process: { | ||
toObj: (num) => num, | ||
fromObj: (num) => ++num | ||
} | ||
}, | ||
"TestNumber2": { | ||
type: "N", | ||
process: { | ||
toObj: (num) => num, | ||
fromObj: (num) => --num | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const obj = schema.convertObjectFromSchema({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestNumber": 5, | ||
"TestNumber2": 5, | ||
"TestString": "Value" | ||
} | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestNumber": 6, | ||
"TestNumber2": 4, | ||
"TestString": "Value" | ||
} | ||
} | ||
}); | ||
}); | ||
it("Processes nested DATE objects.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestDate": { | ||
type: "Date" | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const date = new Date(); | ||
const obj = schema.convertObjectFromSchema({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestDate": date.toISOString() | ||
} | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestDate": date | ||
} | ||
} | ||
}); | ||
}); | ||
}, | ||
convertToSchemaTests: () => { | ||
it("Processes nested objects.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestNumber": { | ||
type: "N", | ||
process: (num) => ++num | ||
}, | ||
"TestNumber2": { | ||
type: "N", | ||
process: (num) => --num | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const obj = schema.convertObjectToSchema({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestNumber": 5, | ||
"TestNumber2": 5, | ||
"TestValue": "String" | ||
} | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestNumber": 6, | ||
"TestNumber2": 4, | ||
"TestValue": "String" | ||
} | ||
} | ||
}); | ||
}); | ||
it("Processes nested DATE objects.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestDate": { | ||
type: "Date" | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const date = new Date(); | ||
const obj = schema.convertObjectToSchema({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestDate": date | ||
} | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestDate": date.toISOString() | ||
} | ||
} | ||
}); | ||
}); | ||
it("Does not add attributes that are not being set.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestNumber": { | ||
type: "N", | ||
process: (num) => ++num | ||
}, | ||
"TestNumber2": { | ||
type: "N", | ||
process: (num) => num ? --num : undefined | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const obj = schema.convertObjectToSchema({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestNumber": 5 | ||
} | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
"TestItem": { | ||
"TestParam": { | ||
"TestNumber": 6 | ||
} | ||
} | ||
}); | ||
}); | ||
}, | ||
convertUpdateToSchemaTests: () => { | ||
it("Processes nested objects in set attribute.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestNumber": { | ||
type: "N", | ||
process: (num) => ++num | ||
}, | ||
"TestNumber2": { | ||
type: "N", | ||
process: (num) => --num | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const obj = schema.convertUpdateObjectToSchema({ | ||
set: { | ||
"TestItem": { | ||
"TestParam": { | ||
"TestNumber": 5, | ||
"TestNumber2": 5, | ||
"TestValue": "Value" | ||
} | ||
} | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
set: { | ||
"TestItem": { | ||
"TestParam": { | ||
"TestNumber": 6, | ||
"TestNumber2": 4, | ||
"TestValue": "Value" | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
it("Processes nested DATE objects in set attribute.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestDate": { | ||
type: "Date" | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const date = new Date(); | ||
const obj = schema.convertUpdateObjectToSchema({ | ||
set: { | ||
"TestItem": { | ||
"TestParam": { | ||
"TestDate": date | ||
} | ||
} | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
set: { | ||
"TestItem": { | ||
"TestParam": { | ||
"TestDate": date.toISOString() | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
it("Processes nested DATE objects in set attribute if explicitly defined.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestDate": { | ||
type: "Date" | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const date = new Date(); | ||
const obj = schema.convertUpdateObjectToSchema({ | ||
set: { | ||
"TestItem.TestParam.TestDate": date | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
set: { | ||
"TestItem.TestParam.TestDate": date.toISOString() | ||
} | ||
}); | ||
}); | ||
it("Sets only the nested object if explicitly defined.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestNumber": { | ||
type: "N", | ||
process: (num) => ++num | ||
}, | ||
"TestNumber2": { | ||
type: "N", | ||
process: (num) => --num | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const obj = schema.convertUpdateObjectToSchema({ | ||
set: { | ||
"TestItem.TestParam.TestNumber": 5, | ||
"TestItem.TestParam.TestNumber2": 5, | ||
"TestItem.TestParam.TestString": "Value" | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
set: { | ||
"TestItem.TestParam.TestNumber": 6, | ||
"TestItem.TestParam.TestNumber2": 4, | ||
"TestItem.TestParam.TestString": "Value" | ||
} | ||
}); | ||
}); | ||
it("Does not set attributes that are not in the object.", () => { | ||
const schema = mapSchemaBuilder("TestItem", { | ||
attributes: { | ||
"TestParam": { | ||
type: "M", | ||
attributes: { | ||
"TestNumber": { | ||
type: "N", | ||
process: (num) => ++num | ||
}, | ||
"TestNumber2": { | ||
type: "N", | ||
process: (num) => --num | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
const obj = schema.convertUpdateObjectToSchema({ | ||
set: { | ||
"TestItem.TestParam.TestNumber": 5 | ||
} | ||
}); | ||
expect(obj).to.deep.equal({ | ||
set: { | ||
"TestItem.TestParam.TestNumber": 6 | ||
} | ||
}); | ||
}); | ||
}, | ||
validationTests: () => { | ||
@@ -15,0 +355,0 @@ it("Returns an error is thrown if the object does not have the appropriate keys.", () => { |
import { MapSchema } from "../../../KeySchema"; | ||
import NormalSchemaBuilder from "../Normal/NormalSchemaBuilder"; | ||
import NormalSchemaBuilder, { UpdateBody } from "../Normal/NormalSchemaBuilder"; | ||
export { MapSchema }; | ||
export declare class MapSchemaBuilder extends NormalSchemaBuilder<MapSchema> { | ||
constructor(key: string, schema: MapSchema); | ||
convertObjectToSchema(baseObject: any): any; | ||
convertObjectFromSchema(baseObject: any): any; | ||
convertUpdateObjectToSchema(updateBody: UpdateBody<any>): any; | ||
} | ||
export default MapSchemaBuilder; |
@@ -18,2 +18,53 @@ "use strict"; | ||
} | ||
convertObjectToSchema(baseObject) { | ||
let returnObj = super.convertObjectToSchema(baseObject); | ||
if (baseObject && baseObject.hasOwnProperty(this.key) && this.schema.attributes) { | ||
let subObject = returnObj[this.key]; | ||
const attKeys = Object.keys(this.schema.attributes); | ||
for (const attributeKey of attKeys) { | ||
const attributeSchema = this.schema.attributes[attributeKey]; | ||
const builder = SchemaBuilder_1.getSchemaBuilder(attributeKey, attributeSchema); | ||
subObject = builder.convertObjectToSchema(subObject); | ||
} | ||
returnObj[this.key] = subObject; | ||
} | ||
return returnObj; | ||
} | ||
convertObjectFromSchema(baseObject) { | ||
let returnObj = super.convertObjectFromSchema(baseObject); | ||
if (baseObject && baseObject.hasOwnProperty(this.key) && this.schema.attributes) { | ||
let subObject = returnObj[this.key]; | ||
const attKeys = Object.keys(this.schema.attributes); | ||
for (const attributeKey of attKeys) { | ||
const attributeSchema = this.schema.attributes[attributeKey]; | ||
const builder = SchemaBuilder_1.getSchemaBuilder(attributeKey, attributeSchema); | ||
subObject = builder.convertObjectFromSchema(subObject); | ||
} | ||
returnObj[this.key] = subObject; | ||
} | ||
return returnObj; | ||
} | ||
convertUpdateObjectToSchema(updateBody) { | ||
let returnObj = super.convertUpdateObjectToSchema(updateBody); | ||
if (!returnObj || !returnObj.set || !this.schema.attributes) { | ||
return returnObj; | ||
} | ||
const setKeys = Object.keys(returnObj.set); | ||
for (const setKey of setKeys) { | ||
const settingObj = returnObj.set[setKey]; | ||
const splitKeys = setKey.split("."); | ||
const mainKey = splitKeys[0]; | ||
if (splitKeys.length === 0 || mainKey !== this.key) { | ||
continue; | ||
} | ||
const remainingKeys = splitKeys.slice(1); | ||
if (this.schema.attributes.hasOwnProperty(remainingKeys[0])) { | ||
const attributeSchema = this.schema.attributes[remainingKeys[0]]; | ||
const builder = SchemaBuilder_1.getSchemaBuilder(remainingKeys[0], attributeSchema); | ||
const convertedObj = builder.convertObjectToSchema(expand(remainingKeys, settingObj)); | ||
returnObj.set[setKey] = retrieveValue(convertedObj, remainingKeys); | ||
} | ||
} | ||
return returnObj; | ||
} | ||
} | ||
@@ -30,3 +81,3 @@ exports.MapSchemaBuilder = MapSchemaBuilder; | ||
const attributeSchema = attributes[attributeKey]; | ||
let builder = SchemaBuilder_1.getSchemaBuilder(attributeKey, attributeSchema); | ||
const builder = SchemaBuilder_1.getSchemaBuilder(attributeKey, attributeSchema); | ||
const foundErrors = (obj) ? builder.validateObjectAgainstSchema(obj) : undefined; | ||
@@ -47,3 +98,3 @@ errors.push(...(foundErrors || [])); | ||
const attributeSchema = attributes[attributeKey]; | ||
let builder = SchemaBuilder_1.getSchemaBuilder(attributeKey, attributeSchema); | ||
const builder = SchemaBuilder_1.getSchemaBuilder(attributeKey, attributeSchema); | ||
const foundErrors = builder.validateUpdateObjectAgainstSchema({ | ||
@@ -61,2 +112,17 @@ set: (obj.set) ? obj.set[key] : undefined, | ||
} | ||
function expand(keys, value) { | ||
if (keys.length === 0) { | ||
return {}; | ||
} | ||
if (keys.length === 1) { | ||
return { [keys[0]]: value }; | ||
} | ||
return { [keys[0]]: expand(keys.slice(1), value) }; | ||
} | ||
function retrieveValue(obj, keys) { | ||
if (keys.length === 0) { | ||
return obj; | ||
} | ||
return retrieveValue(obj[keys[0]], keys.slice(1)); | ||
} | ||
function removeKeyFromBeginning(key, values) { | ||
@@ -63,0 +129,0 @@ if (values) { |
@@ -10,6 +10,7 @@ import NormalSchemaBuilder, { NormalSchema } from "../NormalSchemaBuilder"; | ||
updateValidationTests?: TestExtension<SB, DT>; | ||
makeObjectTests?: TestExtension<SB, DT>; | ||
makeUpdateObjectTests?: TestExtension<SB, DT>; | ||
convertToSchemaTests?: TestExtension<SB, DT>; | ||
convertUpdateToSchemaTests?: TestExtension<SB, DT>; | ||
convertFromSchemaTests?: TestExtension<SB, DT>; | ||
} | ||
export declare function buildNormalSchemaTests<SB extends NormalSchemaBuilder = NormalSchemaBuilder, DT = unknown>(props: NormalSchemaBuilderTestProps<SB, DT>): void; | ||
export declare function checkForErrors(callback: () => string[], expectedErrors?: string[]): void; |
@@ -27,3 +27,2 @@ "use strict"; | ||
describe(NormalSchemaBuilder_1.default.prototype.validateUpdateObjectAgainstSchema.name, () => { | ||
const { updateValidationTests } = props; | ||
it("Tests that an error is thrown if the set object contains a constant item.", () => { | ||
@@ -102,2 +101,3 @@ const schema = schemaBuilder("Test", { constant: true }); | ||
}); | ||
const { updateValidationTests } = props; | ||
if (updateValidationTests) { | ||
@@ -144,2 +144,6 @@ updateValidationTests(schemaBuilder); | ||
}); | ||
const { convertFromSchemaTests } = props; | ||
if (convertFromSchemaTests) { | ||
convertFromSchemaTests(schemaBuilder); | ||
} | ||
}); | ||
@@ -174,3 +178,3 @@ describe(NormalSchemaBuilder_1.default.prototype.convertObjectToSchema.name, () => { | ||
}); | ||
const { makeObjectTests } = props; | ||
const { convertToSchemaTests: makeObjectTests } = props; | ||
if (makeObjectTests) { | ||
@@ -199,3 +203,3 @@ makeObjectTests(schemaBuilder); | ||
}); | ||
const { makeUpdateObjectTests } = props; | ||
const { convertUpdateToSchemaTests: makeUpdateObjectTests } = props; | ||
if (makeUpdateObjectTests) { | ||
@@ -202,0 +206,0 @@ makeUpdateObjectTests(schemaBuilder); |
import { KeySchema } from "../../KeySchema"; | ||
import { UpdateBody } from "../TableService"; | ||
import DateSchemaBuilder from "./Date/DateSchemaBuilder"; | ||
import MapSchemaBuilder from "./Map/MapSchemaBuilder"; | ||
import MultiTypeSchemaBuilder from "./MultiType/MultiTypeSchemaBuilder"; | ||
@@ -15,2 +16,2 @@ import NormalSchemaBuilder from "./Normal/NormalSchemaBuilder"; | ||
} | ||
export declare function getSchemaBuilder(key: string, schema: KeySchema): DateSchemaBuilder | MultiTypeSchemaBuilder | NormalSchemaBuilder<import("../../KeySchema").DynamoSchema<unknown>>; | ||
export declare function getSchemaBuilder(key: string, schema: KeySchema): DateSchemaBuilder | MapSchemaBuilder | MultiTypeSchemaBuilder | NormalSchemaBuilder<import("../../KeySchema").DynamoSchema<unknown>>; |
@@ -66,3 +66,3 @@ "use strict"; | ||
}, | ||
makeObjectTests: () => { | ||
convertToSchemaTests: () => { | ||
it("Tests that the string is slugged.", () => { | ||
@@ -69,0 +69,0 @@ const schema = schemaBuilder("Test", { slugify: true }); |
{ | ||
"name": "@xapp/dynamo-service", | ||
"version": "1.0.0-beta.1", | ||
"version": "1.0.0-beta.2", | ||
"description": "A dynamo help class which will help maintain data integrity.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
441540
8075