Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript Code Contract Libraries?
    primarykey
    data
    text
    <p>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 <a href="http://kinsey.no/blog/index.php/2010/02/03/jscontract-code-contracts-for-javascript/" rel="noreferrer">JsContact</a> but the syntax isn't quite what I had in mind. Is anyone aware of other libraries?</p> <p>I am thinking I want the syntax to be something like</p> <pre><code>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; }; </code></pre> <p>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.</p> <p><strong>UPDATE</strong></p> <p>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.</p> <p>I have also given a little more thought to the API that would make sense, and I am currently thinking something like (contrived examples):</p> <pre><code> 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. } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload