Validation without objects using Joi - javascript

good evening.
I'm having trouble while trying to validate a variable using Joi. I've read this page link
Joi usually validates object through schemas, however, the page I mentioned show that Joi also supports a more "direct" approach to validation. In my case, I'm trying just to validate a variable.
I'm trying the following:
const {error, value} = Joi.number().validate("SAMPLE TEXT");
It's not precisely what I'm trying to do (I'd change "SAMPLE TEXT" for a variable), but It's a good example to show my problem.
What I'd expect from this code is that an error is thrown (because validation should fail), meaning that ** error should not be null or undefined **, however, when trying:
console.log(`Error: ${error}`};
I get:
Error: undefined
Can anyone help me?
[]

Check your package is installed perfectly. I did not find any issue in your code. I have run it properly.
const Joi = require('#hapi/joi'); check this line also

Related

Why am I getting this firebase/firestore error?

Here is my query
const query = messagesRef.where('members', 'in', [`${currentUserId}`]).orderBy('createdAt')
I have created an index as well.
This is the error I get
Error: {"code":"failed-precondition","name":"FirebaseError"}
Which I assume is usually an index thing
Yes, the failed-precondition can be due to a missing index. It can also be manually thrown by a user though with any user-defined description, typically from within a Firebase Function. The category is the only thing that must confine to the convention:
https://cloud.google.com/datastore/docs/concepts/errors
Here's one example of such error common in App-Checked Firebase Functions:
if (context.app == undefined) {
throw new functions.https.HttpsError(
'failed-precondition',
'The function must be called from an App Check verified app.')
}
You may want to check your Function Logs to get the actual error description. If the issue happens locally, then your firestore-debug.log file should contain some useful information. Just to be certain it's actually an index issue or not.
If the issue is happening only on production, you may want to verify the contents of your firestore.indexes.json and compare to what's inside the Firebase Console under https://console.firebase.google.com/u/0/project/<PROJECT-NAME>/firestore/indexes.
If it's actually an index issue, there will be a URL provided inside the logs that you can click which will generate the index for you inside Firestore:
https://firebase.google.com/docs/firestore/query-data/indexing?authuser=0&hl=en#create_a_missing_index_through_an_error_message
I hope this helps track down the issue! I used failed-precondition for several different error types, some unrelated to Firestore or indexes.

Getting an undefined error when trying to set value of input node.js puppeteer

Basically, I'm trying to set the value of an input using node.js puppeteer. Here's the relevant code:
//Enter email
await page.evaluate(() => document.getElementsByName('emailOrPhone')[0].value = 'tester#gmail.com');
I've double checked that the actual selector is correct. As proof, here's a screenshot of me entering document.getElementsByName('emailOrPhone')[0].value into the console of the website, and getting a correct value:
But according to my console, I'm trying to set property 'value' of undefined. What's going on here?
Finally figured it out
I was trying to set the value of the input before it had actually loaded onto the page. I believe this is called a "race condition". Anyway, after I called the method
await page.waitForSelector('input[name="emailOrPhone"]');
and then tried to enter text into the input field, it worked
Check the documentation of the puppeteer, there is the method "type"
You can use it like this:
document.querySelector("[name=emailOrPhone]").type("test#test.test");

Function in validator, not controller giving me an undefined at 'lowerCase()'

I have a form where I'm trying to ensure that case insensitivity is not a problem with an email validation. I have an angular validation called mustMatch that does just what it states, it makes sure that the emails match index for index. However I also need to make sure that case is not an issue. So I ended up creating a secondary function called matchCaseInsensitivity and ultimately that is where I am having a problem because I decided the best way to fix this would be by adding the tolowerCase() filter. When I did this, i was able to bypass my angular mustMatch error messages (which is good), but when I would hit a submit on the form, I would then run into a javascript problem in that 'tolowerCase()' is undefined.
I think that the reason why i'm getting this error is because I have these built in a validator file, not a controller. (not sure if that is anything)
My front end looks like this. Notice the 'match-case-insensitive' => true as my build for the solution
%input-md{ type: "email", "ng-model" => "vm.form.email_confirmation", required: true, 'must-match' => 'register_form["vm-form-email"]', 'match-case-insensitive' => true, 'ng-hide' => 'vm.form.validated_email', autocapitalize: 'off' }
Confirm email address
My mustMatch validation error message is
if attr.mustMatch then addValidation 'mustMatch', 'This field must match the previous value.'
The actual function that triggers the mustMatch error message, I also have it bundled with the 'match-case-insensitive' that I refer to in the front end. This also is in a validation file, not the controller. (for what that is worth....i dunno). (mind you, it is in coffeeScript)
getMatchValue = ->
match = matchGetter($scope)
if (angular.isObject(match) and match.hasOwnProperty('$viewValue'))
match = match.$viewValue
match
$scope.$watch getMatchValue, ->
ctrl.$$parseAndValidate()
return
ctrl.$validators.mustMatch = ->
match = getMatchValue()
if $attrs.matchCaseInsensitive
ctrl.$viewValue.toLowerCase() is match.toLowerCase()
else
ctrl.$viewValue is match
return
I continue to get this TypeError: Cannot read property 'toLowerCase' of undefined at r.$validators.mustMatch. I have been stuck on this for over 2 days, and truthfully have no idea how to solve it. I would really be grateful if someone could take a look and see what they can do.
From the error, it's clear that either ctrl.$viewValue or match (or both) are undefined. Not sure what you want the expected behavior to be in that situation, but here is one possible way you could do it (I'm going to write this in JavaScript):
match = getMatchValue()
if (!(match && ctrl.$viewValue)) {
return false
}
if ($attrs.matchCaseInsensitive) {
return ctrl.$viewValue.toLowerCase() === match.toLowerCase()
}
return ctrl.$viewValue === match

Multiple errors per field in Mongoose validation

Has anyone workaround for getting all errors per field in Mongoose validation? Right now, when some field has an error, validation for that field stops, because of that, you need to resubmit form to see next error.
This behaviour might be fixed in future (https://github.com/Automattic/mongoose/issues/2612), but before that, I would love to see a workaround, which would let me to use validation method, for e.g.:
User.schema.path('hashed_password').validate(function (value) {
if ( ! something)
this.invalidate('password', 'Something is wrong with the password');
});
Note: Using validation method, I can bind error to any field, not just the one validated at the moment. Right now there is no plugin which would let me do that (I have tested mongoose-validator-all and mongoose-validate-all and those plugins are using different strategy).

Validating a command in Vorpal.js

When using the Vorpal.js code library, if I have created a command that looks like:
command-name [strategy]
and I have set the[strategy] command to accept only the values"insert", "update", and "upsert", how do I validate my code within the Vorpal.js framework? I'm guessing I would need to use some sort of validation function to parse the index and log an error message to the console if the index entry is not found. Or I could parse the index for each of the three strings instead. This would require marginally more code but I wonder, which is the most efficient way? Or perhaps people could suggest an implementation that is quicker still? Any suggestions of alternative methodologies would be great.
I am using the current build found at:
Vorpal.js code repository, Github
Vorpal doesn't have any custom validation method, so you can just manually validate it without too much trouble. Something like this would work:
const valids = ['insert', 'update', 'upsert'];
if (valids.indexOf(args.strategy) === -1) {
this.log('Please enter a valid strategy');
cb();
return;
}
Update
Adding a validation method is now on the roadmap for Vorpal.

Categories