Add base64UrlSpec and fix indentation

This commit is contained in:
Dan Schulte 2018-04-11 10:02:09 -07:00
Родитель 56c7abe59e
Коммит 8a56c7a450
15 изменённых файлов: 159 добавлений и 89 удалений

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

@ -0,0 +1,27 @@
// 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 Base64Url encoded ByteArray.
*/
const byteArraySpec: TypeSpec<string> = {
typeName: "Base64Url",
serialize(propertyPath: string[], value: any): string {
if (!value || typeof value.constructor.isBuffer !== "function" || !value.constructor.isBuffer(value)) {
throw new Error(createValidationErrorMessage(propertyPath, value, "a Buffer"));
}
const result: string = value.toString("base64");
let trimmedResultLength = result.length;
while ((trimmedResultLength - 1) >= 0 && result[trimmedResultLength - 1] === "=") {
--trimmedResultLength;
}
return result.substr(0, trimmedResultLength).replace(/\+/g, "-").replace(/\//g, "_");
}
};
export default byteArraySpec;

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

@ -6,14 +6,14 @@ import { TypeSpec, createValidationErrorMessage } from "./typeSpec";
* A type specification that describes how to validate and serialize a ByteArray.
*/
const byteArraySpec: TypeSpec<string> = {
typeName: "ByteArray(Buffer)",
typeName: "ByteArray",
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");
serialize(propertyPath: string[], value: any): string {
if (!value || typeof value.constructor.isBuffer !== "function" || !value.constructor.isBuffer(value)) {
throw new Error(createValidationErrorMessage(propertyPath, value, "a Buffer"));
}
return value.toString("base64");
}
};
export default byteArraySpec;

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

@ -6,14 +6,14 @@ import { TypeSpec, createValidationErrorMessage } from "./typeSpec";
* A type specification that describes how to validate and serialize a Date.
*/
const dateSpec: TypeSpec<string> = {
typeName: "Date",
typeName: "Date",
serialize(propertyPath: string[], value: any): string {
if (!value || (!(value instanceof Date) && (typeof value !== "string" || isNaN(Date.parse(value))))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 format`));
}
return (value instanceof Date ? value : new Date(value)).toISOString().substring(0, 10);
serialize(propertyPath: string[], value: any): string {
if (!value || (!(value instanceof Date) && (typeof value !== "string" || isNaN(Date.parse(value))))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 format`));
}
return (value instanceof Date ? value : new Date(value)).toISOString().substring(0, 10);
}
};
export default dateSpec;

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

@ -6,14 +6,14 @@ import { TypeSpec, createValidationErrorMessage } from "./typeSpec";
* A type specification that describes how to validate and serialize a Date.
*/
const dateTimeRfc1123Spec: TypeSpec<string> = {
typeName: "DateTimeRFC1123",
typeName: "DateTimeRFC1123",
serialize(propertyPath: string[], value: any): string {
if (!value || (!(value instanceof Date) && (typeof value !== "string" || isNaN(Date.parse(value))))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 format`));
}
return (value instanceof Date ? value : new Date(value)).toUTCString();
serialize(propertyPath: string[], value: any): string {
if (!value || (!(value instanceof Date) && (typeof value !== "string" || isNaN(Date.parse(value))))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 format`));
}
return (value instanceof Date ? value : new Date(value)).toUTCString();
}
};
export default dateTimeRfc1123Spec;

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

@ -6,14 +6,14 @@ import { TypeSpec, createValidationErrorMessage } from "./typeSpec";
* A type specification that describes how to validate and serialize a Date.
*/
const dateTimeSpec: TypeSpec<string> = {
typeName: "DateTime",
typeName: "DateTime",
serialize(propertyPath: string[], value: any): string {
if (!value || (!(value instanceof Date) && (typeof value !== "string" || isNaN(Date.parse(value))))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 format`));
}
return (value instanceof Date ? value : new Date(value)).toISOString();
serialize(propertyPath: string[], value: any): string {
if (!value || (!(value instanceof Date) && (typeof value !== "string" || isNaN(Date.parse(value))))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 format`));
}
return (value instanceof Date ? value : new Date(value)).toISOString();
}
};
export default dateTimeSpec;

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

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

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

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

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

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

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

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

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

@ -7,14 +7,14 @@ import { isDuration } from "moment";
* A type specification that describes how to validate and serialize a Date.
*/
const timeSpanSpec: TypeSpec<string> = {
typeName: "TimeSpan",
typeName: "TimeSpan",
serialize(propertyPath: string[], value: any): string {
if (!value || (!isDuration(value) && !(value.constructor && value.constructor.name === "Duration" && typeof value.isValid === "function" && value.isValid()))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `a TimeSpan/Duration`));
}
return value.toISOString();
serialize(propertyPath: string[], value: any): string {
if (!value || (!isDuration(value) && !(value.constructor && value.constructor.name === "Duration" && typeof value.isValid === "function" && value.isValid()))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `a TimeSpan/Duration`));
}
return value.toISOString();
}
};
export default timeSpanSpec;

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

@ -5,24 +5,24 @@
* A type specification that describes how to validate and serialize an object of a given type.
*/
export interface TypeSpec<TSerialized> {
/**
* The name of the type that this TypeSpec validates.
*/
typeName: string;
/**
* The name of the type that this TypeSpec validates.
*/
typeName: string;
/**
* The values that are allowed for this TypeSpec. If this is undefined, then all values of the
* correct type are valid.
*/
allowedValues?: TSerialized[];
/**
* The values that are allowed for this TypeSpec. If this is undefined, then all values of the
* correct type are valid.
*/
allowedValues?: TSerialized[];
/**
* Validate and serialize the provided value into the return type T.
* @param propertyPath The path from the root of the type being serialized down to this
* property.
* @param value The value to validate and serialize.
*/
serialize(propertyPath: string[], value: any): TSerialized;
/**
* Validate and serialize the provided value into the return type T.
* @param propertyPath The path from the root of the type being serialized down to this
* property.
* @param value The value to validate and serialize.
*/
serialize(propertyPath: string[], value: any): TSerialized;
}
/**
@ -32,5 +32,5 @@ export interface TypeSpec<TSerialized> {
* @param expectedConditionDescription A brief description of what type was expected.
*/
export function createValidationErrorMessage(propertyPath: string[], value: any, expectedConditionDescription: string): string {
return `Property ${propertyPath.join(".")} with value ${JSON.stringify(value)} must be ${expectedConditionDescription}.`;
return `Property ${propertyPath.join(".")} with value ${JSON.stringify(value)} must be ${expectedConditionDescription}.`;
}

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

@ -6,15 +6,15 @@ import { TypeSpec, createValidationErrorMessage } from "./typeSpec";
* A type specification that describes how to validate and serialize a Date.
*/
const unixTimeSpec: TypeSpec<number> = {
typeName: "UnixTime",
typeName: "UnixTime",
serialize(propertyPath: string[], value: any): number {
if (!value || (!(value instanceof Date) && (typeof value !== "string" || isNaN(Date.parse(value))))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 format`));
}
const valueDate: Date = (value instanceof Date ? value : new Date(value));
return Math.floor(valueDate.getTime() / 1000);
serialize(propertyPath: string[], value: any): number {
if (!value || (!(value instanceof Date) && (typeof value !== "string" || isNaN(Date.parse(value))))) {
throw new Error(createValidationErrorMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 format`));
}
const valueDate: Date = (value instanceof Date ? value : new Date(value));
return Math.floor(valueDate.getTime() / 1000);
}
};
export default unixTimeSpec;

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

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

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

@ -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 base64UrlSpec from "../../lib/serialization/base64UrlSpec";
describe("base64UrlSpec", () => {
it("should have \"Base64Url\" for its typeName property", () => {
assert.strictEqual("Base64Url", base64UrlSpec.typeName);
});
describe("serialize()", () => {
it("should throw an error when given undefined", () => {
try {
base64UrlSpec.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 Buffer.");
}
});
it("should throw an error when given 5", () => {
try {
base64UrlSpec.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 Buffer.");
}
});
it("should throw an error when given {}", () => {
try {
base64UrlSpec.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 Buffer.");
}
});
it("should return a base64 encoded string with no error when given a Buffer", () => {
assert.strictEqual(base64UrlSpec.serialize(["this", "one", "works"], new Buffer([0, 1, 2, 3, 4])), "AAECAwQ");
});
});
});

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

@ -4,8 +4,8 @@ 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);
it("should have \"ByteArray\" for its typeName property", () => {
assert.strictEqual("ByteArray", byteArraySpec.typeName);
});
describe("serialize()", () => {
@ -14,7 +14,7 @@ describe("byteArraySpec", () => {
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).");
assert.strictEqual(error.message, "Property a.property.path with value undefined must be a Buffer.");
}
});
@ -23,7 +23,7 @@ describe("byteArraySpec", () => {
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).");
assert.strictEqual(error.message, "Property another.property.path with value 5 must be a Buffer.");
}
});
@ -32,7 +32,7 @@ describe("byteArraySpec", () => {
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).");
assert.strictEqual(error.message, "Property another.property.path with value {} must be a Buffer.");
}
});