Bowtie: Simplify Your JSON Data Validation

Are you tired of feeling like you're in a never-ending battle with data validation in your web app? Are you tired of staring at a screen full of indecipherable error messages, wondering where you went wrong? Well, fear not, my fellow developers! Bowtie is here to save the day, like a superhero with a fancy accessory. But don't worry, you don't need to wear a black tie to use bowtie (although it might make your code look more formal). In this blog post, we'll dive into what Bowtie is, how it works, and why it's the superhero sidekick you didn't know you needed. So sit back, relax, and let's put on our Bowties and get started!

What actually Bowtie is?

Bowtie is a program that serves as a "meta-validator" for JSON Schema validation implementations*. It allows users to execute various JSON Schema validation implementations and collect their results.

implementations- it refers to a specific program or library that has been developed to validate JSON Schema.

There are currently many implementations of JSON Schema validation, each written in a different programming language and with its own unique set of features and capabilities. Bowtie is a program that allows users to execute and compare the results of these various implementations to better understand how they interpret and validate JSON Schema.

Bowtie currently supports 15 JSON Schema implementations across 10 programming languages (including JavaScript, go, python, rust, boon, lua, etc).

How does the validation process work?

For each implementation, a harness is created in their respective language, which runs inside a Docker container. This harness enables users to validate the JSON schema validators by running them against official JSON Schema Test Suites and producing results.

harness- block of code that helps to validate implementation against JSON Schema Test Suites

Test Suite- it is a set of tests meant to exercise compliance with the JSON Schema specifications

Bowtie presents these results in two ways. Firstly, users can view the results in the terminal by piping the validation command to bowtie summary.

$ bowtie suite -i lua-jsonschema https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft7/type.json | bowtie summary

Secondly, Bowtie generates an HTML (by piping the validation command to bowtie report) report that shows the test results in a more user-friendly format. The HTML report includes a table that summarizes the test results for each implementation and highlights any failures or errors that occurred during testing.

$ bowtie suite -i lua-jsonschema https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft7/type.json | bowtie report

Quick example

(Assuming that bowtie is installed in the system. Installation guide can be found here) A simple schema might look like this:

{
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "number", "minimum": 0}
    },
    "required": ["name"]
}

This schema defines an object with two properties: name, which must be a string, and age, which must be a number greater than or equal to zero. The required keyword specifies that the "name" property is mandatory.

Once the schema has been defined, it can be used to validate JSON data. Bowtie provides a simple command-line interface that allows you to validate data against a schema. For example, to validate a JSON file called data.json against the schema defined above, you would use the following command:

bowtie validate schema.json data.json

If the data in "data.json" conforms to the schema, Bowtie will output a success message else it will show an error.

Benefits of Bowtie tool

1. Simplified Data Validation - Bowtie provides a simplified syntax for defining data structures, making it easy for developers to write and understand validation rules.

2. Detailed Error Messages - Bowtie provides detailed error messages that make it easy for developers to identify and fix issues with data validation.

3. Lightweight and Open-Source - Bowtie is a lightweight and open-source library that can be easily integrated into web applications.

4. Built-In Validators - Bowtie provides several built-in validators that can be used to enforce additional constraints on the data.

5. Works with JSON-Based Data Formats - Bowtie is designed to work with JSON-based data formats, making it a versatile tool for validating and enforcing data schemas in web applications.

Conclusion

Bowtie is a powerful tool for testing and validating JSON Schema implementations. By supporting multiple JSON Schema implementations across various programming languages, Bowtie allows users to easily compare and contrast how each implementation interprets and validates JSON Schema. With its user-friendly command line interface and HTML reports, Bowtie is a valuable resource for anyone working with JSON Schema and looking to improve the quality and accuracy of their data validation processes.