This commit is contained in:
Dan Schulte 2018-04-11 09:47:29 -07:00
Родитель 960310db88
Коммит 56c7abe59e
4 изменённых файлов: 92 добавлений и 30 удалений

Просмотреть файл

@ -6,14 +6,14 @@ import { TypeSpec, createValidationErrorMessage } from "./typeSpec";
* A type specification that describes how to validate and serialize a boolean.
*/
const booleanSpec: TypeSpec<boolean> = {
typeName: "boolean",
typeName: "boolean",
serialize(propertyPath: string[], value: any): boolean {
if (typeof value !== "boolean") {
throw new Error(createValidationErrorMessage(propertyPath, value, "a boolean"));
}
return value;
serialize(propertyPath: string[], value: any): boolean {
if (typeof value !== "boolean") {
throw new Error(createValidationErrorMessage(propertyPath, value, "a boolean"));
}
return value;
}
};
export default booleanSpec;

Просмотреть файл

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { TypeSpec, createValidationErrorMessage } from "./typeSpec";
/**
* A type specification that describes how to validate and serialize a ByteArray.
*/
const byteArraySpec: TypeSpec<string> = {
typeName: "ByteArray(Buffer)",
serialize(propertyPath: string[], value: any): string {
if (!value || typeof value.constructor.isBuffer !== "function" || !value.constructor.isBuffer(value)) {
throw new Error(createValidationErrorMessage(propertyPath, value, "a ByteArray(Buffer)"));
}
return value.toString("base64");
}
};
export default byteArraySpec;

Просмотреть файл

@ -4,31 +4,31 @@ import * as assert from "assert";
import booleanSpec from "../../lib/serialization/booleanSpec";
describe("booleanSpec", () => {
it("should have \"boolean\" for its typeName property", () => {
assert.strictEqual("boolean", booleanSpec.typeName);
it("should have \"boolean\" for its typeName property", () => {
assert.strictEqual("boolean", booleanSpec.typeName);
});
describe("serialize()", () => {
it("should throw an error when given undefined", () => {
try {
booleanSpec.serialize(["a", "property", "path"], undefined);
assert.fail("Expected an error to be thrown.");
} catch (error) {
assert.strictEqual(error.message, "Property a.property.path with value undefined must be a boolean.");
}
});
describe("serialize()", () => {
it("should throw an error when given undefined", () => {
try {
booleanSpec.serialize(["a", "property", "path"], undefined);
assert.fail("Expected an error to be thrown.");
} catch (error) {
assert.strictEqual(error.message, "Property a.property.path with value undefined must be a boolean.");
}
});
it("should throw an error when given 5", () => {
try {
booleanSpec.serialize(["another", "property", "path"], 5);
assert.fail("Expected an error to be thrown.");
} catch (error) {
assert.strictEqual(error.message, "Property another.property.path with value 5 must be a boolean.");
}
});
it("should return the provided value with no error when given true", () => {
assert.strictEqual(booleanSpec.serialize(["this", "one", "works"], true), true);
});
it("should throw an error when given 5", () => {
try {
booleanSpec.serialize(["another", "property", "path"], 5);
assert.fail("Expected an error to be thrown.");
} catch (error) {
assert.strictEqual(error.message, "Property another.property.path with value 5 must be a boolean.");
}
});
it("should return the provided value with no error when given true", () => {
assert.strictEqual(booleanSpec.serialize(["this", "one", "works"], true), true);
});
});
});

Просмотреть файл

@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import * as assert from "assert";
import byteArraySpec from "../../lib/serialization/byteArraySpec";
describe("byteArraySpec", () => {
it("should have \"ByteArray(Buffer)\" for its typeName property", () => {
assert.strictEqual("ByteArray(Buffer)", byteArraySpec.typeName);
});
describe("serialize()", () => {
it("should throw an error when given undefined", () => {
try {
byteArraySpec.serialize(["a", "property", "path"], undefined);
assert.fail("Expected an error to be thrown.");
} catch (error) {
assert.strictEqual(error.message, "Property a.property.path with value undefined must be a ByteArray(Buffer).");
}
});
it("should throw an error when given 5", () => {
try {
byteArraySpec.serialize(["another", "property", "path"], 5);
assert.fail("Expected an error to be thrown.");
} catch (error) {
assert.strictEqual(error.message, "Property another.property.path with value 5 must be a ByteArray(Buffer).");
}
});
it("should throw an error when given {}", () => {
try {
byteArraySpec.serialize(["another", "property", "path"], {});
assert.fail("Expected an error to be thrown.");
} catch (error) {
assert.strictEqual(error.message, "Property another.property.path with value {} must be a ByteArray(Buffer).");
}
});
it("should return a base64 encoded string with no error when given a Buffer", () => {
assert.strictEqual(byteArraySpec.serialize(["this", "one", "works"], new Buffer([0, 1, 2, 3, 4])), "AAECAwQ=");
});
});
});