Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Code Contract Libraries?

I am just starting up a new web application and I want to implement some form of contract'esque style validation in my JavaScript. I did some quick googling, and came across JsContact but the syntax isn't quite what I had in mind. Is anyone aware of other libraries?

I am thinking I want the syntax to be something like

String.prototype.padLeft = function(c, width) {
  Verify.value(c).isRequired().isNotNull().isChar();
  Verify.value(width).isRequired().isNotNull().isNumber().greaterThan(0);

  ...

  Verify.value(result).isNotNull();

  return result;
};

Although it won't take long to put together my own library that has the syntax/methods I want, if someone else has already done the work and it is close enough, it will save me some time. Thanks in advance.

UPDATE

I won't have time to work on this until this afternoon, so I will give it a few more hours to see if anyone has any recommendations. If not, I will post whatever I create up somewhere as an answer below for other people to reference if they desire.

I have also given a little more thought to the API that would make sense, and I am currently thinking something like (contrived examples):

 function searchUser(firstName, middleInit, lastName) {
   Verify.value(firstName).isString().matching(/\w+/);       // Must have value
   Verify.value(middleInit).whenNotNull().isChar();          // May be null, but not undefined
   Verify.value(lastName).isString().withMinimumLengthOf(2); // Must have value

   ...
 }

 function syncTime(serverTime, now) {
   Verify.value(serverTime).isDate();         // Must have value.
   Verify.value(now).whenDefined().isDate();  // May be undefined, but not null.

 }

My current thought is that tolerating NULL or UNDEFINED values is atypical (at least for me?), as such, rather than explicitly specifying that a value .isNotNull() you would actually disable the rule for .whenDefined() or .whenNotNull() as shown above. I may make .whenNotNull() not error on UNDEFINED, but I see NULL vs. UNDEFINED as an important distinction; we'll see... all other methods will be pretty typical... thoughts? comments?

like image 557
Chris Baxter Avatar asked Dec 15 '10 00:12

Chris Baxter


People also ask

What is a JavaScript library?

Formerly named LiveScript, JavaScript made its stand due to its unique capability of designing network-centric applications. A JavaScript library is a library that contains pre-written, useful JavaScript codes that anyone can use to build different parts of an application without building the code from scratch and wasting time on it.

What is the difference between a JS library and a framework?

In JS libraries, the parent code calls the function that a library offers. In JS frameworks, the framework itself calls the code and uses it in a specific way. It defines the overall application design. Simply put, you can think of JavaScript libraries as a particular app function.

Why should you use a JavaScript library for database management?

Using a JavaScript library makes it easier to handle a document following its content while adding more interactivity. Effective database management is necessary to read, create, delete, edit, and sort data. You can also use sophisticated queries, auto-create tables, synchronize and validate data, and much more.

What is JavaScript and how does it work?

JavaScript libraries and frameworks make website and application development easier with wide-ranging features and functionalities — all thanks to JavaScript’s dynamic, flexible, and engaging features.


1 Answers

Given that no one has recommended any existing libraries, or that I am crazy for thinking this is a good idea I went ahead and threw together a basic library. The code isn't fancy, but it does what I want, and it is reasonably fast to run (approx 40 chained checks per ms in IE).

I settled on a final syntax like:

function syncTime(serverTime, now) {
  Verify.value(serverTime).always().isDate();   // Cannot be undefined or null.
  Verify.value(now).whenDefined().isDate();     // Cannot be null, but must be date when defined.

  //Code
}

function searchForUser(firstName, middleInit, lastName) {
  Verify.value(firstName).always().isString().withMinimumLengthOf(2);  // Cannot be undefined or null.
  Verify.value(lastName).always().isString().withMinimumLengthOf(2);   // Cannot be undefined or null.
  Verify.value(middleInit).whenNotNull().isChar().between('A', 'Z');   // Cannot be undefined, but must be single char string when not null.

  //Code
}

I opted for an explicit 'Must Have Value' via the .always() check, personally I found it nicer to read; but I could see some going another way.

Given that the source is more than I want to post in this answer, please head to this CodePlex Wiki Page if you are interested in the source. I guess it turned in to more of a fluent assertion library; but it does what I need.

Update

I updated the source on the linked CodePlex page above. Specifically, I restructed the Verify class to make use of a 'value context' rather than always building new Verifier objects; improved IE's performance greatly (never was an issue with FireFox or Chrome)... now handles about 100 chained checks per ms in IE.

like image 58
Chris Baxter Avatar answered Sep 21 '22 14:09

Chris Baxter