Skip to content

Commit

Permalink
v.1.0.0 contd
Browse files Browse the repository at this point in the history
* Python version
* fix typos in PHP, JS versions
* all versions have same typecasters/validators
* remove fields,default typecasters
* remove fields,mimetype validators
* update tests
  • Loading branch information
foo123 committed Aug 4, 2022
1 parent 4600d64 commit f2ad958
Show file tree
Hide file tree
Showing 9 changed files with 1,137 additions and 92 deletions.
66 changes: 27 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,6 @@ date.1
// composite typecaster
Formal::typecast('composite', [$typecaster1, $typecaster2, ..]);

// fields typecaster
Formal::typecast('fields', ['field1' => $typecaster1, 'field2' => $typecaster2, ..]);

// default value typecaster
Formal::typecast('default', $defaultValue);

// boolean typecaster
Formal::typecast('bool');

Expand Down Expand Up @@ -201,83 +195,77 @@ Formal::typecast(function($val, $key, $formalInstance) {
// optional validator, only if value is not missing
Formal::validate('optional', $requiredValidator);

// required validator, fails if value is missing
// required validator, fails if value is missing or null
Formal::validate('required');

// fields validator
Formal::validate('fields', ['field1' => $validator1, 'field2' => $validator2, ..], $errMsg = null);

// is numeric validator
Formal::validate('numeric', $args = null, $errMsg = null);
Formal::validate('numeric');

// is object validator
Formal::validate('object', $args = null, $errMsg = null);
Formal::validate('object');

// is array validator
Formal::validate('array', $args = null, $errMsg = null);

// is file validator (PHP only)
Formal::validate('file', $args = null, $errMsg = null);
Formal::validate('array';

// mime-type validator (PHP only)
Formal::validate('mimetype', ['type1', 'type2', ..], $errMsg = null);
// is file validator
Formal::validate('file');

// is empty validator
Formal::validate('empty', $args = null, $errMsg = null);
Formal::validate('empty');

// max items validator
Formal::validate('maxcount', $maxCount, $errMsg = null);
Formal::validate('maxitems', $maxCount);

// min items validator
Formal::validate('mincount', $minCount, $errMsg = null);
Formal::validate('minitems', $minCount);

// max chars validator
Formal::validate('maxlen', $maxLen, $errMsg = null);
Formal::validate('maxchars', $maxLen);

// min chars validator
Formal::validate('minlen', $minLen, $errMsg = null);
Formal::validate('minchars', $minLen);

// max file size validator (PHP only)
Formal::validate('maxsize', $maxSize, $errMsg = null);
// max file size validator
Formal::validate('maxsize', $maxSize);

// min file size validator (PHP only)
Formal::validate('minsize', $minSize, $errMsg = null);
// min file size validator
Formal::validate('minsize', $minSize);

// equals validator
Formal::validate('eq', $otherValueOrField, $errMsg = null);
Formal::validate('eq', $otherValueOrField);

// not equals validator
Formal::validate('neq', $otherValueOrField, $errMsg = null);
Formal::validate('neq', $otherValueOrField);

// greater than validator
Formal::validate('gt', $otherValueOrField, $errMsg = null);
Formal::validate('gt', $otherValueOrField);

// greater than or equal validator
Formal::validate('gte', $otherValueOrField, $errMsg = null);
Formal::validate('gte', $otherValueOrField);

// less than validator
Formal::validate('lt', $otherValueOrField, $errMsg = null);
Formal::validate('lt', $otherValueOrField);

// less than or equal validator
Formal::validate('lte', $otherValueOrField, $errMsg = null);
Formal::validate('lte', $otherValueOrField);

// between values (included) validator
Formal::validate('between', [$minValueOrField, $maxValueOrField], $errMsg = null);
Formal::validate('between', [$minValueOrField, $maxValueOrField]);

// in array of values validator
Formal::validate('in', [$val1, $val2, ..], $errMsg = null);
Formal::validate('in', [$val1, $val2, ..]);

// not in array of values validator
Formal::validate('not_in', [$val1, $val2, ..], $errMsg = null);
Formal::validate('not_in', [$val1, $val2, ..]);

// match pattern validator
Formal::validate('match', $pattern, $errMsg = null);
Formal::validate('match', $pattern);

// match valid email pattern validator
Formal::validate('email', $args = null, $errMsg = null);
Formal::validate('email');

// match valid url pattern validator
Formal::validate('url', $args = null, $errMsg = null);
Formal::validate('url');

// not validator
$validator->_not_();
Expand Down
100 changes: 80 additions & 20 deletions src/js/Formal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ var HAS = Object.prototype.hasOwnProperty,
toString = Object.prototype.toString,
ESC_RE = /[.*+?^${}()|[\]\\]/g,
EMAIL_RE = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
URL_RE = new RegExp('^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$','i');
URL_RE = new RegExp('^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$','i'),
isNode = ("undefined" !== typeof global) && ("[object global]" === toString.call(global)),
isBrowser = ("undefined" !== typeof window) && ("[object Window]" === toString.call(window))
;

function is_numeric(x)
{
Expand All @@ -41,6 +44,38 @@ function is_object(x)
{
return ('[object Object]' === toString.call(x)) && ('function' === typeof x.constructor) && ('Object' === x.constructor.name);
}
async function is_file(x)
{
if (isNode)
{
return await new Promise(function(resolve) {
require('fs').lstat(String(x), function(err, stats) {
resolve(err || !stats ? false : stats.isFile());
});
});
}
else if (isBrowser)
{
return ('File' in window) && (x instanceof File);
}
return false;
}
async function filesize(x)
{
if (isNode)
{
return await new Promise(function(resolve) {
require('fs').lstat(String(x), function(err, stats) {
resolve(err || !stats ? false : stats.size);
});
});
}
else if (isBrowser)
{
return ('File' in window) && (x instanceof File) ? x.size : false;
}
return false;
}
function is_callable(x)
{
return 'function' === typeof(x);
Expand Down Expand Up @@ -286,16 +321,15 @@ class FormalType
}

async t_composite(v, k, m) {
var types = this.inp, l = types.length, i = 0;
while (i < l)
var types = array(this.inp), l = types.length, i = 0;
for (i=0; i<l; ++i)
{
v = await types[i].exec(v, k, m);
++i;
}
return v;
}

async t_fields(v, k, m) {
/*async t_fields(v, k, m) {
if (!is_object(v) && !is_array(v)) return v;
var SEPARATOR = m.option('SEPARATOR'), field, type;
for (field in this.inp)
Expand All @@ -314,7 +348,7 @@ class FormalType
v = defaultValue;
}
return v;
}
}*/

t_bool(v, k, m) {
// handle string representation of booleans as well
Expand Down Expand Up @@ -404,7 +438,7 @@ class FormalValidator
var valid = true;
if (is_callable(this.func))
{
valid = await this.func(v, k, m, missingValue);
valid = !!(await this.func(v, k, m, missingValue));
}
return valid;
}
Expand Down Expand Up @@ -482,7 +516,7 @@ class FormalValidator
return valid;
}

async v_fields(v, k, m, missingValue) {
/*async v_fields(v, k, m, missingValue) {
if (!is_object(v) && !is_array(v)) return false;
var SEPARATOR = m.option('SEPARATOR'), field, validator;
for (field in this.inp)
Expand All @@ -501,7 +535,7 @@ class FormalValidator
}
}
return true;
}
}*/

v_numeric(v, k, m, missingValue) {
var valid = is_numeric(v);
Expand All @@ -521,34 +555,56 @@ class FormalValidator
return valid;
}

async v_file(v, k, m, missingValue) {
var valid = await is_file(v);
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', '') : "\""+k+"\" must be a file!");
return valid;
}

v_empty(v, k, m, missingValue) {
var valid = missingValue || null == v || (is_array(v) || is_object(v) ? !Object.keys(v).length : !String(v).trim().length);
var valid = missingValue || is_null(v) || (is_array(v) ? !v.length : (is_object(v) ? !Object.keys(v).length : !String(v).trim().length));
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', '') : "\""+k+"\" must be empty!");
return valid;
}

v_maxcount(v, k, m, missingValue) {
v_maxitems(v, k, m, missingValue) {
var valid = v.length <= this.inp;
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', this.inp) : "\""+k+"\" must have at most "+this.inp+" items!");
return valid;
}

v_mincount(v, k, m, missingValue) {
v_minitems(v, k, m, missingValue) {
var valid = v.length >= this.inp;
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', this.inp) : "\""+k+"\" must have at least "+this.inp+" items!");
return valid;
}

v_maxlen(v, k, m, missingValue) {
v_maxchars(v, k, m, missingValue) {
var valid = v.length <= this.inp;
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', this.inp) : "\""+k+"\" must have at most "+this.inp+" characters!");
return valid;
}

v_minlen(v, k, m, missingValue) {
v_minchars(v, k, m, missingValue) {
var valid = v.length >= this.inp;
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', this.inp) : "\""+k+"\" must have at least "+this.inp+" characters!");
return $valid;
return valid;
}

async v_maxsize(v, k, m, missingValue) {
var fs = false, valid = false;
fs = await filesize(String(v));
valid = false === fs ? false : fs <= this.inp;
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', this.inp) : "\""+k+"\" must have at most "+this.inp+" bytes!");
return valid;
}

async v_minsize(v, k, m, missingValue) {
var fs = false, valid = false;
fs = await filesize(String(v));
valid = false === fs ? false : fs >= this.inp;
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', this.inp) : "\""+k+"\" must have at least "+this.inp+" bytes!");
return valid;
}

v_eq(v, k, m, missingValue) {
Expand Down Expand Up @@ -671,7 +727,7 @@ class FormalValidator
}
valid = -1 === array(val).indexOf(v);
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', valm) : "\""+k+"\" must not be one of "+valm+"!");
return $valid;
return valid;
}

v_match(v, k, m, missingValue) {
Expand Down Expand Up @@ -901,7 +957,7 @@ class Formal
if (is_array(k))
{
k.forEach(function(kk) {
o[kk] = clone(defaults);
o[kk] = clone(defaults); // clone
});
}
else
Expand All @@ -914,7 +970,7 @@ class Formal
}

async doMergeDefaults(data, defaults, WILDCARD = '*', SEPARATOR = '.') {
if (is_array(data) || is_object(data))
if ((is_array(data) || is_object(data)) && (is_array(defaults) || is_object(defaults)))
{
var keys, key, def, k, kk, n, o, doMerge, i, ok;
for (key in defaults)
Expand Down Expand Up @@ -972,16 +1028,20 @@ class Formal
}
else if (is_null(data[key]) || (is_string(data[key]) && !data[key].trim().length))
{
data[key] = def;
data[key] = clone(def); // clone
}
}
else
{
data[key] = def;
data[key] = clone(def); // clone
}
}
}
}
else if (is_null(data[key]) || (is_string(data) && !data.trim().length))
{
data = clone(defaults); // clone
}
return data;
}

Expand Down
Loading

0 comments on commit f2ad958

Please sign in to comment.