Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.7.0+

Validate a QR Code Request

See more QR Code Examples

Demonstrates the validate operation, which examines a request without producing anything. The scope says how far to look: definition checks the options, capacity also checks that the data fits a QR symbol, and layout also checks the rendered size against the limits. The response has a validationStatus of valid or invalid, a list of checks, and findings[] describing what is wrong. A request that cannot be understood at all, such as one with a misspelled option, still makes the call fail in the usual way.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const fmt = try chilkat.Formats.init();
    defer fmt.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    var i: i32 = 0;
    var n: i32 = 0;

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("target", "symbol") catch {};
    json.updateString("validation.scope", "capacity") catch {};
    json.updateString("payload.text", "https://www.chilkatsoft.com/barcode_overview.asp") catch {};

    fmt.qrCode("validate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("status: {s}\n", .{try json_out.stringOf(alloc, "validationStatus")});
    n = json_out.sizeOfArray("checks");
    i = 0;
    while (i < n) : (i += 1) {
        json_out.setI(i);
        std.debug.print("  {s}: {s}\n", .{ try json_out.stringOf(alloc, "checks[i].name"), try json_out.stringOf(alloc, "checks[i].status") });
    }

    // The same text does not fit version 2 at error-correction level H.
    json.updateInt("encoding.qr.maxVersion", 2) catch {};
    json.updateString("encoding.qr.errorCorrection", "H") catch {};

    fmt.qrCode("validate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("status: {s}\n", .{try json_out.stringOf(alloc, "validationStatus")});
    n = json_out.sizeOfArray("checks");
    i = 0;
    while (i < n) : (i += 1) {
        json_out.setI(i);
        std.debug.print("  {s}: {s}\n", .{ try json_out.stringOf(alloc, "checks[i].name"), try json_out.stringOf(alloc, "checks[i].status") });
    }

    n = json_out.sizeOfArray("findings");
    i = 0;
    while (i < n) : (i += 1) {
        json_out.setI(i);
        std.debug.print("finding: {s} at {s}\n", .{ try json_out.stringOf(alloc, "findings[i].code"), try json_out.stringOf(alloc, "findings[i].path") });
        std.debug.print("  {s}\n", .{try json_out.stringOf(alloc, "findings[i].message")});
    }

    // Sample output:

    // status: valid
    //   definition: passed
    //   capacity: passed
    //   layout: notApplicable
    // status: invalid
    //   definition: passed
    //   capacity: failed
    //   layout: notApplicable
    // finding: capacityExceeded at /payload
    //   the payload does not fit any admitted QR version at the selected error-correction level
}