Posts

conditional subschemas (writing conditions inside a JSON Schema)

Following keywords are used to implement conditional schema, which means validation of JSON will be happening with some defined condition. All these keywords work together to implement the conditional application of a subschema based on the outcome of another subschema. These keywords must not interact with each other across subschema boundaries. In other words, an "if" in one branch of an "allOf" must not have an impact on a "then" or "else" in another branch. There is no default behavior for any of these keywords when they are not present. In particular, they must not be treated as if present with an empty schema, and when "if" is not present, both "then" and "else" must be entirely ignored. if : This keyword's value must be a valid JSON Schema. This keyword's subschema has no direct effect on the overall validation result. Rather, it controls which of the "then" or "else" key

JSON Schema validation keywords with more explanation

JSON schema contains so many keywords, some applies on any Instance and some applies only on Numerics, Strings, Arrays and Objects. All this are separated into multiple types, Any instance type keywords : type : Value of this keywords is String or Array , Here Array elements must be String and unique . Here values must be one of the following types, "null" "boolean" "object" "array" "number" "string" "integer" enum : It contains array of elements with minimum length of 1 and with any value including null . Instance value must be equal to any one of the elements else JSON schema validation will be failed. const : Instance can be any type but instance value must be equal to value of the keyword. Numeric instances keywords (number and integer) : multipleOf : This value need be a number and greater than 0. Instance value must be multiple of keyword's value. maximum : This value

JSON Schema Validation

Intro: I have already used JSON in so many ways and in so many projects but never know about  JSON Schema. So after doing some digging into    draft-handrews-json-schema-validation-01, I have came up with following info, Terminology: The terms "schema", "instance", and "meta-schema" are to be interpreted as defined in the  JSON Schema core specification   [json-schema].  The JSON Schema Core specification is where the terminology for a schema is defined. The terms "applicable" and "attached" are to be interpreted as defined in   Section 3 of the JSON Schema validation specification   [json-schema-validation] .  The JSON Schema Validation specification is the document that defines the valid ways to define validation constraints. This document also defines a set of keywords that can be used to specify validations for a JSON API. In the examples that follow, we’ll be using some of these keywords. The terms "link"

Creating a simple node project

Creating a simple node project: Recently one of my client had a requirement like standalone application which consumes rabbitmq messages from a unique exchange with type topic. This application need to perform some validations on received message and make HTTP call to another server and forwards the updated data. As per my client requirement I have developed standalone java application but I just wanna try the same using nodejs (I am java & javascript developer from the very beginning). In my office we used to follow standard node+express development kit for most of the application. All the application I have worked are developed on top of that development kit with express. This time I want to write my own node application without express to fulfil above requirement. I have ` npm ` already installed in my laptop, So I have used ` npm ` to generate my initial project. npm init This process asks questions and generates package.json , Here steps will be like, [package nam

nodejs/javascript client for Rabbitmq

In previous section, we have created our node project, Now we will discuss development of rabbitmq-nodejs-client. rabbitmq-nodejs-client project helps you to write nodejs-client for rabbitmq connection. Requirements: Running RabbitMQ server nodejs amqp.node To install amqp.node using npm: npm install amqplib Development: `touch index.js` will create a index.js file, and update file with following code, var amqp = require('amqplib/callback_api'); console.log("amqp", amqp); and run index.js as, node index.js if your output looks like, amqp { connect: [Function: connect],   credentials: { plain: [Function], amqplain: [Function], external: [Function] },   IllegalOperationError: { [Function: IllegalOperationError] super_: { [Function: Error] stackTraceLimit: 16 } } } then you have ready to start development, if you got error like `Error: Cannot find module 'amqplib/callback_api'` then install amqplib globally like, npm i

Deleting a variable from the window object (Not possible to delete if var is used)

Image
Most of the times all javascript applications deals with objects and their properties. I used empty a property which I longer needed. After that I came to know about  delete  operator which used to remove a property from an object. While using delete operator I got doubt like, is it possible to delete a variable? So the answer to the question depends on how the global variable or property is defined. If it is created using var, it cannot be deleted.                var global = 32;           // global is a variable created using 'var'                delete global;               // returns false                console.log(global)      // global is still 32 If it is created without var, it can be deleted.                global = 1;                    // global is a variable created without using 'var'                delete global;               // return true                console.log(global);     // error, global is not defined

Simple way to use environment variables in nodejs

This page helps you to know about environment variable usage in nodejs. Nodejs contains a global object process which contains a property env  object. Nodejs updates all environment variables into env  on boot-up. We can check all environment variables by logging. console.log(process.env); We can even load variables from external .env  files, by using an npm module called dotenv . npm install dotenv --save Just by simply including below statement at the start of load file, require('dotenv').load(); It loads all .env variables into process.env object. Sample .env file: ENV=dev PORT=3030 DBUSER=root DBPASSWORD=root