README
Clockwork
Clockwork is a client-side validation utilities library. It can be used natively or as part of any javascript framework, and offers 25+ validation rules out of out the box.
Installation
Pull it via npm
npm i @primitivesocial/clockwork --save
Usage
import {_v} from "@primitivesocial/clockwork";
let name = 'primitive';
if(_v.is_string(name) && _v.min(name, 3))
window.alert('name is valid');
Available Rules
:warning: All date values must be of format YYYY-MM-DD (example: '2020-01-15')
afterValidates if a given value (date string) is after another value (date string)
let d = '2020-5-1';
after(d, '2020-1-1'); // returns true
after_or_equalValidates if a given value (date string) is after or equal to another value (date string)
let d = '2020-5-1';
after_or_equal(d, '2020-1-1'); // returns true
alphaValidates if a given value (string) contains only letters
let str = 'clockwork';
alpha(str); // returns true
alpha_dashValidates if a given value (string) contains only letters, dashes and underscores
let str = 'primtivesocial-clock_work';
alpha_dash(str); // returns true
alpha_numericValidates if a given value (string) contains only letters and numbers
let str = 'clockwork123';
alpha_numeric(str); // returns true
beforeValidates if a given date value (date string) is before another value (date string)
let d = '2020-5-1';
before(d, '2020-5-5'); // returns true
before_or_equalValidates if a given value (date string) is before or equal to another value (date string)
let d = '2020-5-1';
before_or_equal(d, '2020-1-1'); // returns true
booleanValidates that a given value is boolean
let str = 'clockwork';
let isValid = false;
boolean(str); // returns false
boolean(isValid); // returns true
dateValidates if a given value (date string) is a valid date
let d = '2020-5-1';
date(d); // returns true
differentValidates if a given value (numbers, strings, array and objects) is different from another value
let str = 'clockwork';
let str2 = 'primitive'
different(str, str2); // returns true
ends_withValidates that a given value (string) ends with a specific string
let str = 'clockwork';
ends_with(str, 'work'); // returns true
integerValidates if a given value is an integer
let str = 'clockwork';
let amount = 100;
integer(str); // returns false
integer(amount); // returns true
is_arrayValidates if a given value is an array
let arr = [ 'foo', 'bar'];
let amount = 100;
is_array(arr); // returns true
is_array(amount); // returns false
is_inValidates if a given value (string) is exists in an array, or a comma separated string
let str = 'foo';
is_in(str, ['foo', 'bar']); // returns true
is_in(str, 'foo,bar') // returns true
is_stringValidates if a given value is a string
let str = 'foo';
is_string(str); // returns true
jsonValidates if a given value is a json object
let obj = { foo: 'bar'};
json(obj); // returns true
lengthValidates the length of a given value (string and arrays)
length('foo', 3); // returns true
length(['foo', 'bar'], 2); // returns true
matches_regexValidates if a given value matches a specific regex
let email = "elie@leadwithprimitive.com";
matches_regex(email, "^\\S+@\\S+[\\.][0-9a-z]+