Firefly Semantics Validator API Basics

Image by Domingo Trejo from Pixabay

Scenario

We have a PurchaseOrder class that has been annotated with Firefly Semantics Validator decorators.

Now we want to learn how to use the Firefly Semantics ValidatorAPI

To validate:

  • A single property
  • A single PurchaseOrder instance.
  • Multiple PurchaseOder instances.

Approach

Create a new PurchaseOrder instance:

const po: PurchaseOrder = new PurchaseOrder(
{
sku: 'skuABC',id: '123',purchaseDate: new Date('2021-11-22'),receiptDate: new Date('2021-11-28'),quantity: 2,});

Single Purchase Order Property Validation

let valid: boolean = validateProperty(po, 'sku');assert.equal(valid, true, 'valid should be true');po.sku = null;valid = validateProperty(po, 'sku');assert.equal(valid, false, 'valid should be false');

Single Purchase Order Instance Validation

To validate a single instance we use validate :

const OE: ObjectErrors = validate(po);let errors: ValidationError[] = OE.errors;assert.equal(OE.valid, false, 'valid should be false');assert.equal(errors.length,1,'There should be one error for the SKU property');//console.log(errors[0].message);assert.equal(errors[0].message.includes('sku'),true,'The invalid property is the sku propertyy');

Validation of Multiple Purchase Order Instances

To validate multiple instances we place them in an array and use validateN([po1, po2, po3]) :

const OES: ObjectErrors[] = validateN([po]);//console.log(OES);assert.equal(OES.length, 1, 'There should be one error');assert.equal(OES[0].valid, false, 'Valid should be false');errors = OES[0].errors;assert.equal(errors.length,1,'There should be one error for the SKU property');//console.log(errors[0].message);assert.equal(errors[0].message.includes('sku'),true,'The invalid property is the sku property');

Demo

Related Concepts

Founder of Firefly Semantics Corporation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Design 003

Master The Skills Of Map() in JavaScript And Be Successful

Using the Angular CLI Component Module Flag

How to format date in certain timezone with momentjs

Jenkins for Node.js App on AWS EC2 Part 2: Creating a Node.js App

Promise Mate: Delegating asynchronous results in JavaScript

Building a Video Chat Application With Socket.io and TypeScript

Create a Temperature converter using NextJs, Tailwind CSS & Strapi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation

More from Medium

Build CRUD on RESTful API with MongoDB — Javascript, Part4 — POST and CREATE.

JavaScript Control Flow & Error Handling

I built a tool to curate my own code template library