I come from a php background, where the validation libraries Iām used to, just return _true_ or _false_ (which is awesome), then I do whatever I want from there. So, I opted to use the āManual validationā here (isEmail precisely), and it worked as expected. (Iām using in a Nativescript application BTW)
Problem is with the second parameter, āoptionsā, whose properties are not mentioned at all in the documentation.
I just ended up setting allow_display_name, allow_utf8_local_part and require_tld to true without even knowing what they mean.
Iād appreciate if these properties (and others like allow_negative_sign_placeholder, host_whitelist and host_blacklist) are explained.
EDIT::
I tried to use the _decorators_ way as shown in the first example, and it worked. BUT console.log("validation failed. errors: ", errors) just outputs '_validation failed. errors: [object Object]_' in the terminal. So, I really want to stick with the āManual validation"
PROTIP: Use console.log(JSON.stringify(myObject, null, 4)); to log the whole array/object with all nested properties. Or even better - use util.inspect():
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null}))
// alternative shortcut
console.log(util.inspect(myObject, false, null))
My background is also php, and don't afraid of asynchronous nature of nodejs platform. There are a lot of benefits in using them. Go ahead and use code this way:
async save(object: T) {
const validationErrors = await this.validate(object);
if (validationErrors.length > 0)
throw new Error("validation failed");
}
Using async/await syntax mades code look and understand better.
In the case if you still want to use sync validation you can use validateSync method:
async save(object: T) {
const validationErrors = this.validateSync(object);
if (validationErrors.length > 0)
throw new Error("validation failed");
}
but keep in mind that in this case you can loose extra functionality that custom validators can provide. But its actually not a problem if you don't create custom validators.
Regarding you comment about undocumented options. You can find documentation in validator package which is used by this library.
btw really good validation library never return you just true or false. True or false used in the trivial validation cases. The really powerful validation frameworks in php also return array of validation error details.
great PROTIP, @19majkel94. I use that everywhere now. Thanks @pleerock, I found everything I needed from the validator documentation.
I couldn't figure out how to get an async method to work though, like you illustrated. I kept getting an error _ERROR SyntaxError: Unexpected token '*'. Expected an opening '(' before a function's parameter list_ , so I gave up and stuck with
private input: string;
private verify() {
let pageinputs = new PageInputs;
pageinputs.text = this.input;
validate(pageinputs).then(errors => {
if (errors.length > 0) {
dialogs.alert({
title: '',
message: JSON.stringify(errors, null, 4),
okButtonText: 'OK'
});
} else {
dialogs.alert(this.input);
}
});
}
I'd however like to you use async like you recommended, if you can throw more light.
the async method I used that kept throwing the error:
private async verify() {
let pageinputs = new PageInputs;
pageinputs.text = this.input;
let errors = await validate(pageinputs);
if (errors.length > 0)
throw new Error("validation failed");
}
try to install newer version of node
@pleerock. I updated node to v7 and things broke. the CLI was saying something about _fibers.node is missing. Try reinstalling node-fibers_ .so, I went down to v6.0, but after building, I still get the _Unexpected token '*'_ error. Must I be on v7 for this error to go away? or is something wrong with the code I posted here?
Im using v6.6.0 and it works fine there. can you try to install latest version of node 6, it must work with it fine
@pleerock, I now have node 6.9.1. But I still get the same _JS ERROR SyntaxError: Unexpected token '*'. Expected an opening '(' before a function's parameter list._ error
hmmm then I guess its something related to your environment and typescript version are you using. Try to open an issue in typescript repo.
@pleerock ...ok, I'll do that