Skip to content

Commit

Permalink
v.1.0.0 contd, fix some edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
foo123 committed Aug 2, 2022
1 parent f1407b1 commit 4600d64
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
27 changes: 15 additions & 12 deletions src/js/Formal.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,27 @@ else if (!(name in root)) /* Browser/WebWorker/.. */
/* module factory */ function ModuleFactory__Formal(undef) {
"use strict";

var HAS = Object.prototype.hasOwnProperty, toString = Object.prototype.toString,
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');

function is_string(x)
{
return ('string' === typeof(x)) || ('[object String]' === toString.call(x));
}
function is_numeric(x)
{
return !isNaN(+x);
}
function is_string(x)
{
return ('string' === typeof(x)) || ('[object String]' === toString.call(x));
}
function is_array(x)
{
return ('[object Array]' === toString.call(x));
}
function is_object(x)
{
return ('[object Object]' === toString.call(x));
return ('[object Object]' === toString.call(x)) && ('function' === typeof x.constructor) && ('Object' === x.constructor.name);
}
function is_callable(x)
{
Expand Down Expand Up @@ -295,7 +296,7 @@ class FormalType
}

async t_fields(v, k, m) {
if (!is_object(v)) return v;
if (!is_object(v) && !is_array(v)) return v;
var SEPARATOR = m.option('SEPARATOR'), field, type;
for (field in this.inp)
{
Expand Down Expand Up @@ -409,7 +410,7 @@ class FormalValidator
}

async v_and(v, k, m, missingValue) {
var valid = await this.inp[0].exec(v, k, m, missingValue) && await this.inp[1].exec(v, k, m, missingValue);
var valid = (await this.inp[0].exec(v, k, m, missingValue)) && (await this.inp[1].exec(v, k, m, missingValue));
return valid;
}

Expand Down Expand Up @@ -470,19 +471,19 @@ class FormalValidator
var valid = true;
if (!missingValue)
{
valid = await this.inp.exec(v, k, m);
valid = await this.inp.exec(v, k, m, false);
}
return valid;
}

v_required(v, k, m, missingValue) {
var valid = !missingValue && (null != v);
var valid = !missingValue && !is_null(v);
if (!valid) throw new FormalException(!empty(this.msg) ? this.msg.replace('{key}', k).replace('{args}', '') : "\""+k+"\" is required!");
return valid;
}

async v_fields(v, k, m, missingValue) {
if (!is_object(v)) return false;
if (!is_object(v) && !is_array(v)) return false;
var SEPARATOR = m.option('SEPARATOR'), field, validator;
for (field in this.inp)
{
Expand Down Expand Up @@ -1090,12 +1091,14 @@ class Formal
{
KEY_ = root.concat(key);
KEY = KEY_.join(SEPARATOR);
err = null;
try {
valid = await validator.exec(null, KEY, this, true);
} catch (e) {
if (e instanceof FormalException)
{
valid = false;
err = e.message;
}
else
{
Expand All @@ -1104,7 +1107,7 @@ class Formal
}
if (!valid)
{
this.err.push(new FormalError(this.option('missing_value_msg').replace('{key}', KEY).replace('{args}', ''), KEY_));
this.err.push(new FormalError(empty(err) ? this.option('missing_value_msg').replace('{key}', KEY).replace('{args}', '') : err, KEY_));
}
return;
}
Expand Down
6 changes: 4 additions & 2 deletions src/php/Formal.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public function v_optional($v, $k, $m, $missingValue)
$valid = true;
if (!$missingValue)
{
$valid = $this->inp->exec($v, $k, $m);
$valid = $this->inp->exec($v, $k, $m, false);
}
return $valid;
}
Expand Down Expand Up @@ -1128,14 +1128,16 @@ private function doValidate($data, $validator, $key = array(), $root = array(),
{
$KEY_ = array_merge($root, $key);
$KEY = implode($SEPARATOR, $KEY_);
$err = null;
try {
$valid = $validator->exec(null, $KEY, $this, true);
} catch (FormalException $e) {
$valid = false;
$err = $e->getMessage();
}
if (!$valid)
{
$this->err[] = new FormalError(str_replace(array('{key}', '{args}'), array($KEY, ''), $this->option('missing_value_msg')), $KEY_);
$this->err[] = new FormalError(empty($err) ? str_replace(array('{key}', '{args}'), array($KEY, ''), $this->option('missing_value_msg')) : $err, $KEY_);
}
return;
}
Expand Down

0 comments on commit 4600d64

Please sign in to comment.