This commit is contained in:
Dan Schulte 2018-04-16 16:05:38 -07:00
Родитель 03bbb48c78
Коммит 84c526d7d8
2 изменённых файлов: 186 добавлений и 18 удалений

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

@ -1,7 +1,9 @@
// 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";
import { TypeSpec, createValidationErrorMessage, createValidationWarningMessage } from "./typeSpec";
import { PropertyPath } from "./propertyPath";
import { SerializationOptions, log } from "./serializationOptions";
import { HttpPipelineLogLevel } from "../httpPipelineLogLevel";
/**
* A type specification that describes how to validate and serialize a Date.
@ -9,18 +11,40 @@ import { PropertyPath } from "./propertyPath";
const dateTimeSpec: TypeSpec<string, Date> = {
specType: "DateTime",
serialize(propertyPath: PropertyPath, value: Date | string): string {
serialize(propertyPath: PropertyPath, value: Date | string, options: SerializationOptions): string {
let result: 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 DateTime format`));
if (options && options.serializationStrictTypeChecking) {
const errorMessage: string = createValidationErrorMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 DateTime format`);
log(options, HttpPipelineLogLevel.ERROR, errorMessage);
throw new Error(errorMessage);
} else {
log(options, HttpPipelineLogLevel.WARNING, createValidationWarningMessage(propertyPath, value, `an instanceof Date or a string in ISO8601 DateTime format`));
}
result = value as any;
} else {
result = (value instanceof Date ? value : new Date(value)).toISOString();
}
return (value instanceof Date ? value : new Date(value)).toISOString();
return result;
},
deserialize(propertyPath: PropertyPath, value: string): Date {
if (!value || typeof value !== "string") {
throw new Error(createValidationErrorMessage(propertyPath, value, `a string in ISO8601 DateTime format`));
deserialize(propertyPath: PropertyPath, value: string, options: SerializationOptions): Date {
let result: Date;
if (!value || typeof value !== "string" || isNaN(Date.parse(value))) {
if (options && options.deserializationStrictTypeChecking) {
const errorMessage: string = createValidationErrorMessage(propertyPath, value, `a string in ISO8601 DateTime format`);
log(options, HttpPipelineLogLevel.ERROR, errorMessage);
throw new Error(errorMessage);
} else {
log(options, HttpPipelineLogLevel.WARNING, createValidationWarningMessage(propertyPath, value, `a string in ISO8601 DateTime format`));
}
result = value as any;
} else {
result = new Date(value);
}
return new Date(value);
return result;
}
};

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

@ -2,7 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.
import * as assert from "assert";
import dateTimeSpec from "../../lib/serialization/dateTimeSpec";
import { serializeTest } from "./specTest";
import { serializeTest, deserializeTest } from "./specTest";
describe("dateTimeSpec", () => {
it("should have \"DateTime\" for its typeName property", () => {
@ -11,7 +11,7 @@ describe("dateTimeSpec", () => {
describe("serialize()", () => {
describe("with strict type-checking", () => {
function dateTimeSerializeWithStrictTypeCheckingTest(args: { propertyPath?: string[], value: Date | string, expectedResult: string | Error }): void {
function dateTimeSerializeWithStrictTypeCheckingTest(args: { propertyPath?: string[], value: Date | string, expectedResult: string | Error, expectedLogs?: string[] }): void {
serializeTest({
typeSpec: dateTimeSpec,
propertyPath: args.propertyPath,
@ -19,28 +19,33 @@ describe("dateTimeSpec", () => {
serializationStrictTypeChecking: true
},
value: args.value,
expectedResult: args.expectedResult
expectedResult: args.expectedResult,
expectedLogs: args.expectedLogs
});
}
dateTimeSerializeWithStrictTypeCheckingTest({
value: <any>undefined,
expectedResult: new Error(`Property a.property.path with value undefined must be an instanceof Date or a string in ISO8601 DateTime format.`)
value: undefined as any,
expectedResult: new Error(`Property a.property.path with value undefined must be an instanceof Date or a string in ISO8601 DateTime format.`),
expectedLogs: [`ERROR: Property a.property.path with value undefined must be an instanceof Date or a string in ISO8601 DateTime format.`]
});
dateTimeSerializeWithStrictTypeCheckingTest({
value: <any>false,
expectedResult: new Error(`Property a.property.path with value false must be an instanceof Date or a string in ISO8601 DateTime format.`)
value: false as any,
expectedResult: new Error(`Property a.property.path with value false must be an instanceof Date or a string in ISO8601 DateTime format.`),
expectedLogs: [`ERROR: Property a.property.path with value false must be an instanceof Date or a string in ISO8601 DateTime format.`]
});
dateTimeSerializeWithStrictTypeCheckingTest({
value: <any>5,
expectedResult: new Error(`Property a.property.path with value 5 must be an instanceof Date or a string in ISO8601 DateTime format.`)
value: 5 as any,
expectedResult: new Error(`Property a.property.path with value 5 must be an instanceof Date or a string in ISO8601 DateTime format.`),
expectedLogs: [`ERROR: Property a.property.path with value 5 must be an instanceof Date or a string in ISO8601 DateTime format.`]
});
dateTimeSerializeWithStrictTypeCheckingTest({
value: "hello world!",
expectedResult: new Error(`Property a.property.path with value "hello world!" must be an instanceof Date or a string in ISO8601 DateTime format.`)
expectedResult: new Error(`Property a.property.path with value "hello world!" must be an instanceof Date or a string in ISO8601 DateTime format.`),
expectedLogs: [`ERROR: Property a.property.path with value "hello world!" must be an instanceof Date or a string in ISO8601 DateTime format.`]
});
dateTimeSerializeWithStrictTypeCheckingTest({
@ -53,5 +58,144 @@ describe("dateTimeSpec", () => {
expectedResult: "2011-10-05T14:48:00.000Z"
});
});
describe("without strict type-checking", () => {
function dateTimeSerializeWithoutStrictTypeCheckingTest(args: { propertyPath?: string[], value: Date | string, expectedResult: string | Error, expectedLogs?: string[] }): void {
serializeTest({
typeSpec: dateTimeSpec,
propertyPath: args.propertyPath,
options: {
serializationStrictTypeChecking: false
},
value: args.value,
expectedResult: args.expectedResult,
expectedLogs: args.expectedLogs
});
}
dateTimeSerializeWithoutStrictTypeCheckingTest({
value: undefined as any,
expectedResult: undefined as any,
expectedLogs: [`WARNING: Property a.property.path with value undefined should be an instanceof Date or a string in ISO8601 DateTime format.`]
});
dateTimeSerializeWithoutStrictTypeCheckingTest({
value: false as any,
expectedResult: false as any,
expectedLogs: [`WARNING: Property a.property.path with value false should be an instanceof Date or a string in ISO8601 DateTime format.`]
});
dateTimeSerializeWithoutStrictTypeCheckingTest({
value: 5 as any,
expectedResult: 5 as any,
expectedLogs: [`WARNING: Property a.property.path with value 5 should be an instanceof Date or a string in ISO8601 DateTime format.`]
});
dateTimeSerializeWithoutStrictTypeCheckingTest({
value: "hello world!",
expectedResult: `hello world!`,
expectedLogs: [`WARNING: Property a.property.path with value "hello world!" should be an instanceof Date or a string in ISO8601 DateTime format.`]
});
dateTimeSerializeWithoutStrictTypeCheckingTest({
value: "2011-10-05T14:48:00.000Z",
expectedResult: "2011-10-05T14:48:00.000Z"
});
dateTimeSerializeWithoutStrictTypeCheckingTest({
value: new Date("2011-10-05T14:48:00.000Z"),
expectedResult: "2011-10-05T14:48:00.000Z"
});
});
});
describe("deserialize()", () => {
describe("with strict type-checking", () => {
function dateTimeDeserializeWithStrictTypeCheckingTest(args: { propertyPath?: string[], value: string, expectedResult: Date | Error, expectedLogs?: string[] }): void {
deserializeTest({
typeSpec: dateTimeSpec,
propertyPath: args.propertyPath,
options: {
deserializationStrictTypeChecking: true
},
value: args.value,
expectedResult: args.expectedResult,
expectedLogs: args.expectedLogs
});
}
dateTimeDeserializeWithStrictTypeCheckingTest({
value: undefined as any,
expectedResult: new Error(`Property a.property.path with value undefined must be a string in ISO8601 DateTime format.`),
expectedLogs: [`ERROR: Property a.property.path with value undefined must be a string in ISO8601 DateTime format.`]
});
dateTimeDeserializeWithStrictTypeCheckingTest({
value: false as any,
expectedResult: new Error(`Property a.property.path with value false must be a string in ISO8601 DateTime format.`),
expectedLogs: [`ERROR: Property a.property.path with value false must be a string in ISO8601 DateTime format.`]
});
dateTimeDeserializeWithStrictTypeCheckingTest({
value: 5 as any,
expectedResult: new Error(`Property a.property.path with value 5 must be a string in ISO8601 DateTime format.`),
expectedLogs: [`ERROR: Property a.property.path with value 5 must be a string in ISO8601 DateTime format.`]
});
dateTimeDeserializeWithStrictTypeCheckingTest({
value: "hello world!",
expectedResult: new Error(`Property a.property.path with value "hello world!" must be a string in ISO8601 DateTime format.`),
expectedLogs: [`ERROR: Property a.property.path with value "hello world!" must be a string in ISO8601 DateTime format.`]
});
dateTimeDeserializeWithStrictTypeCheckingTest({
value: "2011-10-05T14:48:00.000Z",
expectedResult: new Date("2011-10-05T14:48:00.000Z")
});
});
describe("without strict type-checking", () => {
function dateTimeDeserializeWithoutStrictTypeCheckingTest(args: { propertyPath?: string[], value: string, expectedResult: Date | Error, expectedLogs?: string[] }): void {
deserializeTest({
typeSpec: dateTimeSpec,
propertyPath: args.propertyPath,
options: {
deserializationStrictTypeChecking: false
},
value: args.value,
expectedResult: args.expectedResult,
expectedLogs: args.expectedLogs
});
}
dateTimeDeserializeWithoutStrictTypeCheckingTest({
value: undefined as any,
expectedResult: undefined as any,
expectedLogs: [`WARNING: Property a.property.path with value undefined should be a string in ISO8601 DateTime format.`]
});
dateTimeDeserializeWithoutStrictTypeCheckingTest({
value: false as any,
expectedResult: false as any,
expectedLogs: [`WARNING: Property a.property.path with value false should be a string in ISO8601 DateTime format.`]
});
dateTimeDeserializeWithoutStrictTypeCheckingTest({
value: 5 as any,
expectedResult: 5 as any,
expectedLogs: [`WARNING: Property a.property.path with value 5 should be a string in ISO8601 DateTime format.`]
});
dateTimeDeserializeWithoutStrictTypeCheckingTest({
value: "hello world!" as any,
expectedResult: `hello world!` as any,
expectedLogs: [`WARNING: Property a.property.path with value "hello world!" should be a string in ISO8601 DateTime format.`]
});
dateTimeDeserializeWithoutStrictTypeCheckingTest({
value: "2011-10-05T14:48:00.000Z",
expectedResult: new Date("2011-10-05T14:48:00.000Z")
});
});
});
});