Arrow Down
Web Development

How to add schema into a rails app?

Nisha Gopinath Menon
-

To validate our API response and persistent data, we’ve been making use of JSON schema. It’s helped us bring order to our database and saved us a whole lot of time. JSON, an uncomplicated data interchange format, stands for “JavaScript Object Notation.” It started as a notation for the world wide web. Since JSON is based on JavaScript and JavaScript exists in most web browsers, it’s effortless to support there. However, it has proved to be simple enough and useful enough that it is now used in many other instances that go beyond web surfing.

Imagine JSON schema as the spec of the JSON data. It essentially enables you to define how your JSON data must look. When it comes to information across different web-services, it has fast turned out to be the most extensively used transport and serialization mechanism. The format, from its conception initially, has garnered wide and swift praise for being non-verbose and simple.

What’s a Schema?

The simplest way to explain a schema is as a means to define the content, structure and to a certain level, the semantics of XML docs. A rule is given for every node or element in a document to which it must conform. To make it unnecessary to handle the edge cases in the application logic constraints are defined at this level. This is a strong tool. This was not present in the original JSON specification. However, efforts were made to design one later on.

Why do we need a Schema?

One of the major things that made Rails stand out from other frameworks that were similar was that it read your database schema (the tables that were there and the fields those tables had) as the application began and used it to determine the attributes that must appear on the models. Other frameworks came with huge mapping files one had to write out by hand tediously, or buy a tool to do it for you, and then edit it by hand. Rails was remarkable. As the app starts up, all this was done at run-time. And basically, Schema is what helps validate user input at the language/protocol level upfront if you have an unambiguous schema, rather than at the application’s implementation level.

The JSON Schema specification is split into three areas:

  • JSON Schema Core: Where the terminology for a schema is defined is known as the JSON Schema Core specification.
  • JSON Schema Validation: This happens to be the document that sets the right ways to explain constraints in validation. A bunch of keywords which could be put to use to specify the validations when it comes to a JSON API are also defined by this documentation.
  • JSON Hyper-Schema: Of the JSON Schema spec, this is another extension wherein, the hypermedia and hyperlink-related keywords happen to be defined. One noteworthy benefit of following a schema standard is that no matter what the language which might implement the schema, one can be sure about the basic implementation.

What's the benefit of JSON schema?

To start with, it's never a bad idea to define your data properly. Below are a few more advantages:

  • So we don’t screw up the database, a JSON schema validates our JSON data structure.
  • The single rule can be applied anywhere also assists your client when it comes to validating the data.
  • Especially when it comes to REST-like API, it helps you validate your API’s response.
  • To boot, if you happen to use Swagger, it does integrate with it.

Now when it comes to applying JSON schema to Rails model validation, there are two gems that we have found helpful to validate JSON schema. Firstly, the activerecordjsonvalidator, the Rails validator implementation is based on json-schema. And then there is the Ruby validator implementation of JSON schema called json-schema. Hope this proves helpful.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Need help with product design or development?

Our product development experts are eager to learn more about your project and deliver an experience your customers and stakeholders love.

Nisha Gopinath Menon
Bangalore