I'm using Aurelia-validation, and I've created a validation-controller that can on demand run validate( object: myObj, propertyName: "myProp", rules : MyRules)
Documentation:
https://aurelia.io/docs/plugins/validation#validation-controller
For some reason I get, what I can only describe as a conflict, in my validation result object. The only rule I validate on is "required()", and it returns true. However, the result object as a whole returns false. Why is that?
Take a look (using my real data):
Take a close look to your "valid" type. One is Boolean and the other is a String! Check if you can take control of that!
I suppose it has something to do with chrome dev tools, because it seem to happen only when I inspect it while it's running, and not when I console.log() the values and let it finish, Then they correspond to each other. Oh well!
Related
I have been facing issues with the use of u128.add(a, b) function. The two u128 values do not get added and I am afraid I am doing something wrong. I have checked LEARN-NEAR github page for sample projects and even changed my code to follow the patterns used, however the values don't get added.
signWithFunds(amount: u128): bool {
assert(context.sender, "Petition must be signed by an account");
assert(this.isFunded, `not a funded petition`);
assert(u128.ge(amount, this.minFundAmount),
`amount provided is less than minimum funding amount: at least ${asNEAR(this.minFundAmount)} required to sign this petition`);
const currentTotal = this.funding;
this.funding = u128.add(amount, currentTotal);
this.signature.push(context.sender);
return true;
}
model.ts
main.ts
aspect test file
test result showing unexpected behaviour
From the images, it looks like the test aren't receiving the expected values. The test receives 0, but expected some other values. I don't think there's anything wrong with the u128add function in itself.
From the test, you are calling a function that relies on Context's deposit, I think you need to add that to your test *(it("should sign a funded ...."), as well:
VMContext.setAttached_deposit(someDeposit)
Second, signWithFunds is relies on this.founding as well, which I believe is the funding in the petition itself. Maybe petitions[0] in your test isn't the newly created petition? We need to look at the beforeEach function to make sure, because otherwise, you are adding a new petition to the array, but you're referencing an older one.
I discovered that the 7th line of signWithFunds should have been this.funding.set(u128.add(amount, currentTotal));
Model.find(ids).each { |model| model.is_active = false } unless ids.empty?
Here, is model.save method necessary or not ? Because without that also, it's working.
Column is defined like this
t.integer :is_active, limit: 1
The short answer to your question is: Yes.
Running model.is_active = false, by itself, will not persist the change in the database. Try re-fetching the database record after running model.is_active = false, and you'll see that the value has not actually changed; you also need to run model.save to do that.
However, for scenarios like this (assuming you want to immediately save the change to the record!) there's a more concise way of saving the data, instead of running two commands:
model.update_attribute(:is_active, false)
This will update the value in the database, if validations pass, and also run any callbacks (e.g. after_save).
If you want to update the value without running validations and callbacks (which is typically faster, but more "dangerous") then you can instead use:
model.update_column(:is_active, false)
In Ruby, I'm attempting to pass a boolean result to a method which accepts as a string as a parameter. This is as an experiment.
fileexists = File.file?("#{$fileLocation}")
puts File.file?("#{$fileLocation}")
puts fileexists
puts fileexists.to_s
This will result in:
true
true
true
Now if I attempt to call a method which accepts a string and pass this parameter in a number of ways.
slack(message: "#{fileexists}")
Results in the error message.
'message' value must be a String! Found TrueClass instead.
Which confuses me as I understand that Ruby evaluates anything within "" as a String. So placing a TrueClass object within a placeholder, should effectively cast this value to a string.
So let's try something slightly different:
slack(message: "#{fileexists.to_s}")
This results in the same error.
'message' value must be a String! Found TrueClass instead.
Now this is where things GET REALLY WEIRD!!
slack(message: "#{fileexists.to_s} ")
slack(message: "#{fileexists} ")
If I add a single bit of whitespace to the end of the string after the placeholder, it passes, and a slack message is sent my way, displaying 'true'.
I understand I may be asking for a bit of 'Crystal-ball' insight here as
I don't have the implementation of the 'slack' method, and this may be a result of the way that's implemented.
Does Ruby check types of params as they're passed like this?
Is that a Ruby standard error message you might receive, or a custom error thrown by the slack() method?
The dependency you are using, fastlane, auto-converts values that are passed into the actions (your call to slack).
The reason for this is that parameters in fastlane can also be specified via the commandline, so conversion is necessary. It converts your value of "true" to a boolean automatically because there is no Boolean class in ruby and the type of parameters is specified by giving it the name of a class, so it automatically converts "true" to a boolean. The offending line in the code is here.
As you can see in the code above, a workaround would be to do slack(message: "#{fileexists.to_s.capitalize}") or slack(message: fileexists ? "t" : "f"). Anything really as long as you avoid yes, YES, true, TRUE, no, NO, false, and FALSE
I understand I may be asking for a bit of 'Crystal-ball' insight here as I don't have the implementation of the 'slack' method, and this may be a result of the way that's implemented.
Sounds like youre using a lib (gem) which contains the methods slack, you can check the gem code location running gem which gem_name on your console.
Does Ruby check types of params as they're passed like this?
No
Is that a Ruby standard error message you might receive, or a custom error thrown by the slack() method?
Custom Error
As Jorg W Mittag stated this looks like a misimplementation of slack method when trying to deserialize, and then checking the types. You could fix the slack method on the gem by contributing to this gem, monkeypatch it or you can try to hack it the way it is... this last onde depends on how slack was implemented, maybe adding an extra pair of quotes, like "\"#{fileexists}\""
PS: You don't have to embbed the string inside another string if you're going to use it as it is, like fileexists = File.file? $fileLocation , this should work.
I'm only guessing here because we don't know what the method definition of slack is expecting an un-named String, but you're passing a hash.
slack(fileexists.to_s)
I have an Ember object that has as attribute a boolean field that I want to validate with ember-changeset-validations.
It is the typical "Agree to terms" checkbox.
Here is the hbs code:
{{one-way-checkbox changeset.agree_terms
class="form-control"
update=(action (mut changeset.agree_terms))}}
{{#each changeset.error.agree_terms.validation as |error|}}
<span class="text-danger help-block m-l-3">{{t (concat 'bookings.error.' error)}}</span>
{{/each}}
I have a validations file, where the particular validation for this member of changeset is:
agree_terms: validateFormat({
regex: /^(true)$/,
message: 'You need to agree on Terms!'
})
Thing is, for some reason this never validates to TRUE correctly, I always get the error message...any ideas what I'm doing wrong here?
This is a bit tricky; but I figured that out. ember-change-set-validations do make use of ember-validators underneath. Since; you are using validateFormat; the following code is being run at the end. If you look at the link I have provided; there is a check !canInvoke(value, 'match') causes you the problem. Since the checked value of a checkbox is a boolean and you cannot invoke match on booleans you always end up having an invalid validation.
What can you do? Well; I am not an expert at ember-change-set-validations and ember-validators but it is pretty easy to write a custom validator. This is what I did in the following twiddle. It seems to be working pretty fine.
What I understood is; it is not a good idea to use boolean values with format type validator (it clearly does not work); I only wished it was documented. I hope this helps you.
I have a method that checks the signature of a request is valid.
The method returns a boolean:
_ true: valid signature
_ false: signature is not valid
I am not sure about the best name for it (maybe also because I am not an English native speaker).
I am not sure among:
_ checkSignature
_ isSignatureValid
Which one do you think is better and why or maybe you have a better suggestion.
Thanks,
Dan
isSignatureValid( ... ). It tells you what the method is going to return, which is nice. Also, it doesn't make a promise one way or another as far as caching goes (checkSignature implies to me that you will do all the math to check the signature when I call it, and you might not need to repeat all that work).
I prefer
hasValidSignature()
since isSignatureValid() doesn't semantically make sense because the request isn't a signature, it has a signature. I suppose you're going to be using this in an if statement so doesn't this make more sense?
if (request.hasValidSignature()) {
...
}
Furthermore, if you want to check if request is valid itself, then this would be more appropriate
if (request.isValid()) {
...
}