How to use the @plumier/core.errorMessage.UnableToInstantiateModel function in @plumier/core

To help you get started, we’ve selected a few @plumier/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github plumier / plumier / packages / kernel / src / converter.ts View on Github external
if (Array.isArray(expectedType)) throw createConversionError(value, expectedType[0], path)
    const TheType = expectedType as Class
    
    //get reflection metadata of the class
    const reflection = reflect(TheType)
    //check if the value is possible to convert to model
    if (!isConvertibleToObject(value)) throw createConversionError(value, TheType, path)

    //sanitize excess property to prevent object having properties that doesn't match with declaration
    //traverse through the object properties and convert to appropriate property's type
    let result: any;
    try {
        result = new TheType()
    }
    catch (e) {
        const message = errorMessage.UnableToInstantiateModel.format(TheType.name)
        if (e instanceof Error) {
            e.message = message + "\n" + e.message
            throw e
        }
        else throw new Error(message)
    }
    for (let x of reflection.properties) {
        const val = convert((value as any)[x.name], path.concat(x.name), x.type, converters)
        if (val === undefined) {
            delete result[x.name]
        }
        else {
            result[x.name] = val
        }
    }
    return result;