gristlabs / ts-interface-checker

Runtime library to validate data against TypeScript interfaces.
Apache License 2.0
323 stars 18 forks source link

IErrorDetail should be exported #29

Closed mrzap0 closed 4 years ago

mrzap0 commented 4 years ago

It is currently not possible to pass an IErrorDetail value to another function without downgrading it to any.

eg.

import { Checker } from "ts-interface-checker";

export function implementsInterface(typeName: string, checker: Checker, value: any): boolean {
    const result = checker.validate(value);
    if (result === null) {
        return true;
    } else {
        console.error(`ERROR: Value does not implement '${typeName}':\n${formatCheckerError(result)}`);
        return false;
    }
}

function formatCheckerError(error: any /* IErrorDetail not exported from ts-interface-checker */, prefix = '\n  '): string {
    let s = `${error.path} ${error.message`;

    if (error.nested) {
        for (const nested in error.nested) {
            s += prefix + formatCheckerError(nested, prefix + '  ');
        }
    }

    return s;
}
dsagal commented 4 years ago

I think #27 would fix it. I was waiting for some feedback, but will merge in soon.

zenflow commented 4 years ago

@mrzap0 A workaround for now is to import { IErrorDetail } from 'ts-interface-checker/dist/util', but careful when you upgrade the package, because this internal file can change or be moved.

dsagal commented 4 years ago

This is fixed in v0.1.12.

mrzap0 commented 4 years ago

Confirmed