Skip to content

validateAll returns error when parseData returns std::nullopt #11

Open
@bedrich-schindler

Description

@bedrich-schindler

We have following code (simplified for the purpose of example):

// ...
libyang::Context *context;
// ...

// ...
YangModule::YangModule() {
    this->context = new libyang::Context(std::nullopt, libyang::ContextOptions::SetPrivParsed);
}
// ...

/*
 * Validates the data tree against the schema.
 *
 * @param dataTreeJson The data tree to validate
 * @return `true` if the data tree is valid, `false` otherwise
 */
bool YangModule::validateData(const std::string &dataTreeJson) const {
    // Errors are automatically truncated every function call
    this->clearErrors();

    try {
        std::optional<libyang::DataNode> dataNode = this->context->parseDataMem(
            dataTreeJson,
            libyang::DataFormat::JSON,
            libyang::ParseOptions::ParseOnly,
            libyang::ValidationOptions::Present
        );

        validateAll(dataNode, libyang::ValidationOptions::Present);
    } catch (...) {
        return false;
    }

    // Errors are automatically truncated every function call
    return this->getErrors().empty();
}

If parseDataMem (respectively parseData in new version) returns std::nullopt instead of DataNode, and you pass it to the validateAll, libyang returns following error libyang[0]: Invalid argument tree (lyd_validate_all()).

I can provide example schema later, but the problem might be that std::optional<DataNode> Context::parseData returns std::nullopt when !tree. Colleague that use libyang (not libyang-cpp) says it works fine in C:

lyd_parse_data_mem(ctx, "{}", LYD_JSON, LYD_PARSE_ONLY, 0, &dnode);
// lyd_parse_data_mem(ctx, "", LYD_XML, LYD_PARSE_ONLY, 0, &dnode);
lyd_validate_all(&dnode, ctx, LYD_VALIDATE_PRESENT, NULL);

If he passes NULL instead of empty object, it the returns the same error as I mentioned before.

We think that C++ implementation is not correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions