本文整理匯總了TypeScript中io-ts/lib/PathReporter.PathReporter.report方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript PathReporter.report方法的具體用法?TypeScript PathReporter.report怎麽用?TypeScript PathReporter.report使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io-ts/lib/PathReporter.PathReporter
的用法示例。
在下文中一共展示了PathReporter.report方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: getterFunction
function dispatchResponse<T, R>(getterFunction: GetterFunction<T, R>, decodedParams: t.Validation<T>): Promise<any> {
if (decodedParams.isRight()) {
return getterFunction(db, augur, decodedParams.value);
} else {
throw new Error(`Invalid request object: ${PathReporter.report(decodedParams)}`);
}
}
示例2: ValidationError
export function validate<RequestT, TypeT extends t.Type<RequestT> = t.Type<RequestT>>(iotsType: TypeT, body: any): RequestT {
const result = iotsType.decode(body)
if (result.isLeft()) {
throw new ValidationError(PathReporter.report(result))
} else if (result.isRight()) {
return result.value
}
}
示例3: it
it('validates the serialized JSON via io-ts', () => {
const full_test: smoke_test_struct_interface.FullTest = JSON.parse(String(fs.readFileSync(path.resolve(__dirname, './smoke_test_struct_serialized.json'))));
const success_validation = iots.validate(full_test, smoke_test_struct_interface.FullTest_IO);
const success_report = PathReporter.report(success_validation);
assert.deepEqual(success_report, [
'No errors!',
]);
ThrowReporter.report(success_validation);
// Pay attention to the Variant types first. -- @sompylasar
// TODO(@sompylasar): Test more field values.
assert.isTrue(iots.is(full_test.q, smoke_test_struct_interface.C5TCurrentVariant_T9228482442669086788_IO));
function handleVariantQCC(variantQCC: smoke_test_struct_interface.MyFreakingVariant) {
assert.isTrue(iots.is(variantQCC, smoke_test_struct_interface.MyFreakingVariant_IO));
assert.isTrue(iots.is(variantQCC, smoke_test_struct_interface.MyFreakingVariant_VariantCase_A_IO));
const variantQCCA = (variantQCC as smoke_test_struct_interface.MyFreakingVariant_VariantCase_A).A;
if (variantQCCA) {
assert.strictEqual(variantQCCA.a, (1 << 30), 'full_test.q.c.a is not (1 << 30)');
}
else {
assert.isOk(false, 'full_test.q.c is not Variant<A,B,B2,C,Empty> case A');
}
}
function handleVariantQCD(variantQCD: smoke_test_struct_interface.MyFreakingVariant) {
assert.isTrue(iots.is(variantQCD, smoke_test_struct_interface.MyFreakingVariant_IO));
assert.isTrue(iots.is(variantQCD, smoke_test_struct_interface.MyFreakingVariant_VariantCase_Y_IO));
const variantQCDY = (variantQCD as smoke_test_struct_interface.MyFreakingVariant_VariantCase_Y).Y;
if (variantQCDY) {
// The enum E value SOME (0).
assert.strictEqual(variantQCDY.e, 0, 'full_test.q.d.e is not enum E value SOME (0)');
}
else {
assert.isOk(false, 'full_test.q.d is not Variant<A,X,Y> case Y');
}
}
function handleVariantQ(variantQ: smoke_test_struct_interface.C5TCurrentVariant_T9228482442669086788) {
assert.isTrue(iots.is(variantQ, smoke_test_struct_interface.C5TCurrentVariant_T9228482442669086788_VariantCase_C_IO));
const variantQC = (variantQ as smoke_test_struct_interface.C5TCurrentVariant_T9228482442669086788_VariantCase_C).C;
if (variantQC) {
handleVariantQCC(variantQC.c);
handleVariantQCD(variantQC.d);
}
else {
assert.isOk(false, 'full_test.q is not Variant<A,B,B2,C,Empty> case C');
}
}
handleVariantQ(full_test.q);
});
示例4: Error
export const validateConfigurationBlocks = (configurationBlocks: ConfigurationBlock[]) => {
const validationMap = {
isHosts: t.array(t.string),
isString: t.string,
isPeriod: t.string,
isPath: t.string,
isPaths: t.array(t.string),
isYaml: t.string,
};
for (const [index, block] of configurationBlocks.entries()) {
const blockSchema = configBlockSchemas.find(s => s.id === block.type);
if (!blockSchema) {
throw new Error(
`Invalid config type of ${block.type} used in 'configuration_blocks' at index ${index}`
);
}
const interfaceConfig = blockSchema.configs.reduce(
(props, config) => {
if (config.options) {
props[config.id] = t.union(config.options.map(opt => t.literal(opt.value)));
} else if (config.validation) {
props[config.id] = validationMap[config.validation];
}
return props;
},
{} as t.Props
);
const runtimeInterface = createConfigurationBlockInterface(
t.literal(blockSchema.id),
t.interface(interfaceConfig)
);
const validationResults = runtimeInterface.decode(block);
if (validationResults.isLeft()) {
throw new Error(
`configuration_blocks validation error, configuration_blocks at index ${index} is invalid. ${
PathReporter.report(validationResults)[0]
}`
);
}
}
};
示例5: createConfigurationBlockInterface
request.payload.map(async (block: ConfigurationBlock) => {
const assertData = createConfigurationBlockInterface().decode(block);
if (assertData.isLeft()) {
return {
error: `Error parsing block info, ${PathReporter.report(assertData)[0]}`,
};
}
const { blockID, success, error } = await libs.configurationBlocks.save(
request.user,
block
);
if (error) {
return { success, error };
}
return { success, blockID };
})