parris / iz Goto Github PK
View Code? Open in Web Editor NEWBasic validation regexs (and such) for javascript
License: Other
Basic validation regexs (and such) for javascript
License: Other
const rules = {
name: [
{
rule: 'required',
error: 'You must specify a name'
}
]
}
are(rules).for({ name: '' }).valid // true
Any thought on making iz available via bower for client-side usage?
How is this valid?
var iz = require("iz")
iz("[email protected] foo").email().required().valid // true
https://tonicdev.com/56d7fb5074168d0d00297044/56d7fb5074168d0d00297045
Validating a field against another one (say a start date before an end date) is tricky.
One can pass the entire model as the args
argument, but it's not easy to understand.
It will be very helpful if the validators could be called with two parameters:
If there is args
, they are still passed after the value. Something like:
validators.validate = function(value, args*, values) {..}
Sometimes it's useful to be able to validate presence of a value separately to validating it's format. We are using this to support validation of mandatory fields.
Suggested syntax...
iz(value).present()
result = iz('').anArray();
console.log(result.valid); // prints true
Thinking of returning a promise from the provided callback
argument provided to addValidator
. For instance, a unique
validator for usernames..
var rules = {
code: [{
'rule': 'required',
'error': 'is not be empty'
}],
// params: {
// fieldcode: [{
// 'rule': 'required',
// 'error': 'is not be empty'
// }]
// }
};
var date = {code: "fff", params: [{fieldcode: "fff"}]}
how validate a field in array ? i want validate date.params[x].fieldcode ?waht should i do
what i got in mind is something like using your group validation logic:
// My rules, I can look at the keys and inspect the errors
rules = {
cost: iz(costErrors).decimal(),
age: iz(ageErrors).int().between(17, 10000),
producer: {
id: iz(producerErrors).int()
}
};
are(rules).validFor({
cost: 100,
age: 18,
producer: {
id: 1001
}
});
Are seems to ignore the fact that field should be ignored if it doesn't have a required validator.
const name = [
{
rule: 'string',
error: 'Name must be a string'
},
{
rule: 'required',
error: 'Name cannot be empty.'
},
]
const promoCode = [
{
rule: 'string',
error: 'Promo code must be a string.'
},
{
rule: 'minLength',
args: [4],
error: 'Promo Code must be at least 4 characters long.'
},
{
rule: 'maxLength',
args: [12],
error: 'Promo Code can be at most 12 characters.'
},
{
rule: 'notMatch',
args: [REGEX_INVALID_PROMO],
error: 'Promo code contains invalid characters.'
}
]
const rules = {name, promoCode}
are(rules).for({ name: 'PACK1' }).valid // false
Returns valid = false with the following errors even though promoCode is not required.
"promoCode": [
"Promo code must be a string.",
"Promo Code must be at least 4 characters long.",
"Promo Code can be at most 12 characters."
]
Trying to run this with IE11, i think it fails on some ES proxy issue (im using https://github.com/GoogleChrome/proxy-polyfill).
Problem code:
https://github.com/parris/iz/blob/v2.2.2/src/are.js#L81
How that line looks compiled:
currentIz = (_currentIz = currentIz)[rule.rule].apply(_currentIz, _toConsumableArray(args));
Error message:
Unhandled promise rejection TypeError: Unable to get property 'apply' of undefined or null reference
So, currentIz[rule.rule] is undefined.
Would you recommend another proxy polyfill?
Hi there, minLen and maxLen should be minLength and maxLength in the doc.
Thanks,
Brice
Given that this can be useful with NodeJS, it would be great if there was a validator for MongoDB ObjectID:
https://docs.mongodb.org/manual/reference/method/ObjectId/
Great library by the way ๐
could only find things like:
empty()
blank()
alphaNumeric()
but I want to check if the value is of type string.
The ip validators seems to accept any string of letters:
iz('foo').ip().valid; // Retuns true
iz('asdf').ip().valid; // Retuns true
iz('qwertyuiop').ip().valid; // Retuns true
I found the regex iz.ip() was using and it does accept all those strings. The IPv4 part of the regex works correctly, so I assume there is an issue in the (long) IPv6 regex.
/(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/.test('foo'); // Returns false
/(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/.test('0.0.0.0'); // Returns true
iz.email(null) === true
This caused a really weird error in my application. I don't think it's desired behavior.
var rules = {
"myDate": [
{
"rule": "blank",
"error": "Additional insured cannot be blank"
},
{
"rule": "date",
"error": "Additional insured must be valid date"
},
],
};
const result = are(rules).for({
myDate: "09/23/2012"
});
this throws an exception
/home/aleksandar/Code/theguarantors.com/node_modules/iz/lib/utils/nameOfClass.js:6
if (obj.constructor && obj.constructor.displayName) {
^
TypeError: Cannot read property 'constructor' of undefined
at izGetObjectClass (/home/aleksandar/Code/theguarantors.com/node_modules/iz/lib/utils/nam
eOfClass.js:6:10)
param to izDate
is undefined
.
Apologies if I misunderstood something, but didn't have issues with other validators.
I'm currently comparing some of the most popular validation libraries, specifically chai and validator, but also came across iz on SO. From what I can tell, chai/validator seems to cover all of the examples in the README, and a bunch of additional ones too. Could you explain what makes iz a better choice than these two libraries? Have I missed something? Thank you
Thought it would be useful to have a betweenLength validation so that you don't have to use both the minLength and maxLength validations.
Something like:
iz.betweenLength(val, min, max); // val (str or arr) is greater than or equal to min and shorter than or equal to max
Happy to do a pull request if you'd accept the idea.
Thanks!
e.g.
const iz = require('iz')
const validators = require('iz/lib/validators')
iz.register(validators)
console.log(iz(2.3).multiple(0.01).valid)
logs false
instead of true
.
This is because 2.3 % 0.01
is 0.009999999999999775
in JavaScript ๐คฆโโ๏ธ
I'm trying to use the are
syntax with a ruleset. looks good so far but how can I access the error message?
var iz = require("iz");
var test = {
description: "",
value: 2343
};
var rules = {
description: [
{
"rule": "required",
"error": "You must specify a description"
}
],
value: [
{
"rule": "required",
"error": "value is missing"
},
{
"rule": "number",
"error": "value must be a number"
},
{
"rule": "between",
"args": [0, 1000000],
"error": "value must be between 0 and 1000000"
},
]
};
console.log(iz.are(rules).validFor(test)); // false
Hi! I get the following error while installing package via yarn (yarn add iz):
error An unexpected error occurred: "https://registry.yarnpkg.com/iz/-/iz-2.1.2.tgz: invalid tar file".
Tested on clean project (yarn init, yarn add iz). As the yarn issue says (yarnpkg/yarn#1619):
Reporting as of May 14, 2017, that yarn publish seems to be borking things. npm publish works though
So, looks like the solution is to bump the version and to republish iz with npm.
For example:
var rules = {
'address.city': [
{
'rule': 'required',
'error': 'You must specify a city'
},
{
'rule': 'minLength',
'args': [5],
'error': 'The city name length must be more than 5'
}
],
'address.street': [
{
'rule': 'required',
'error': 'You must specify a street'
},
{
'rule': 'minLength',
'args': [5],
'error': 'The street name length must be more than 5'
}
]
};
var are = require('iz').are;
var validationObject = are(rules);
validationObject.validFor(
{
address: {
city: 'Test',
street: "Street test name"
}
}
);
var invalidFields = validationObject.getInvalidFields();
console.log(invalidFields);
Console log:
invalidFields
{
'address.city': [ 'The city name length must be more than 5' ],
'address.street': [ 'The street name length must be more than 5' ]
}
I expect method getInvalidFields
will return only one incorrect field (city)
When using izEqual on a null value, valueType is set to 'object' and then when doing the check on value.equals it throws:
Uncaught TypeError: Cannot read property 'equals' of null
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.