Comments (21)
In this instance you can use a message function on the rule, it is passed the message
and the parameters
and you can return a string.
See: https://github.com/tmpfs/async-validate/blob/master/test/fixtures/schema/message-function.js
from async-validate.
I agree with your proposition, but it means that I'll need to define all error messages for all fields, and I really don't what to do that.
What I want is just overriding default error message, in a global way. So for me it's a small issue, because field name shouldn't be always displayed in error message.
from async-validate.
I agree it should be easier to do, the current design assumes that the more frequent use case is to indicate the field in the error message, I still feel that is the best default behaviour.
I'll have a think about it and see if I can come up with a better solution.
from async-validate.
Okay, thanks for your answer.
If I remember, the problem was probably somewhere here (you're passing arguments, here the field name) : https://github.com/tmpfs/async-validate/blob/master/lib/rule.js#L73-L76
from async-validate.
So if I add an option, let's say {replace: false}
and ignore argument replacement when set will that suffice for your use case?
I can't think of a situation when people would want to mix styles and if they do they can define message as a function
as noted above.
from async-validate.
Yeah it could be a solution to add an option ! But can't it be easier for you to edit your format function to not add field name automatically ?
from async-validate.
As I said before I think the most common use case is to include the field name; remember this isn't just for client-side forms where error messages are shown in context it is a generic solution that can be used for all sorts of validation, for example a web service that validates input data that replies with a message of REQUIRED !
is not particularly useful because it does not say what field was required. Does that make sense to you?
from async-validate.
I see what you meant and in fact, yes, people will generally want to display field name or no field name at all.
So your {replace: false}
proposal should be fine !
from async-validate.
So I ended up implementing this as the option {literal: true}
to indicate that messages should be passed through literally.
Updated options documentation here:
https://github.com/tmpfs/async-validate#schema
Test spec:
https://github.com/tmpfs/async-validate/blob/master/test/spec/message-literal.js
Available in the registry as [email protected]
.
I am closing this as implemented, re-open if you find a bug.
Thanks for your input!
from async-validate.
Very nice, thanks a lot for your fix, I'll try this soon :)
from async-validate.
I tried your solution, it's fine but here is a "but" (yeah, sorry).
With {literal: true}
, the field name is not displayed anymore in error message, but any other variables are not interpreted (for example; the min
rule will print cannot be less than %s
).
I know that it can be tricky for you to deal with this, since the user doesn't have control on these field's rules, except if he define one, but maybe we can find a solution ?
Thanks in advance !
from async-validate.
In this case I think you would need to supply a function to handle that case (the mixed scenario we discussed) above, I think literal behaviour won't call the function at the moment.
I think modifying the behaviour to be: pass through literally unless overriden (there is a message string or function on the rule) should do the trick.
I'll take another look.
from async-validate.
Actually no it looks like a custom message function would be called, I'll add a test case for it.
from async-validate.
So when a custom message is defined on the rule as a function it is invoked and used (I've updated the literal test spec to check).
So you can set the literal
option to get the behaviour you initially wanted and then when you have a requirement for parameter replacement define a message on the rule as a function like this:
https://github.com/tmpfs/async-validate/blob/master/test/fixtures/schema/message-literal.js#L6-L8
Clearly that function can be shared when you have lots of messages that you want to perform replacement on.
from async-validate.
Okay thanks for your answer !
I tried to combine literal
+ message function and it seems that literal
option is ignored.
Maybe I just need to override message function as you said, ignore name parameter and everything should work ?
Here is a working example : https://tonicdev.com/ersefuril/56fcd50f7a27701100df8307
from async-validate.
It certainly should work, look at the test spec which tests for both cases:
https://github.com/tmpfs/async-validate/blob/master/test/spec/message-literal.js
The first is passed through literally and the second is with parameters replaced. If you find a bug with the behaviour please supply a test case that reproduces the issue, the link above does not contain enough fields to combine behaviours:
var descriptor = {
type: 'object',
fields: {
product:{type: "string", required: true, whitespace: true, message: errorMessage}
}
};
Only defines a single field. So to be clear the literal
option will only pass through the literal
message when the rule does not contain a custom message or function, this is what allows you to bypass replacement when you are supplying messages that do not require replacement (the original issue) and allows you to override for situations where you want parameters to be replaced (for example the min
, max
messages).
from async-validate.
Okay I was wrong because it seems that tonicdev doesn't take the latest version of your lib, so we cannot consider my previous example.
I just wrote down some tests, how can I easily give them to you ?
from async-validate.
Just paste the code as markdown code blocks into this issue please.
from async-validate.
Okay, I took some time on my side to think about all this stuff.
So, for me there are 3 uses cases :
- Display custom message with string replacement (ex: REQUIRED %s ! -> REQUIRED myField)
This is the default behavior - Display custom message without any string replacement (ex: REQUIRED !)
To solve this, I need to use{literal: true}
- Display custom message with string replacement BUT without field name (ex: Should have %s chars min ! -> Should have 10 chars min)
To solve this, I need to create a custom message function that will only inject variables I need
var fn = function(msg, params) {
// Remove first params (= name of the field)
var paramsWithoutField = params.splice(1, params.length - 1);
return util.format.apply(util, [msg].concat(paramsWithoutField));
};
// Declare your field rule like this...
job: {type: 'string', required: true, whitespace: true, min: 10, message: fn}
Do you agree with these propositions ? If yes, all case are effectively covered.
from async-validate.
Yes that seems correct, I realize that it might not be ideal but I am certain you can achieve what you want with the existing functionality.
At the moment I don't have any strong ideas on how to improve the message logic and maintain the existing default message behaviour.
from async-validate.
Alright, since all cases can be covered by writing a custom function or playing with the configuration, everything should be fine.
Thanks for your support, again !
from async-validate.
Related Issues (20)
- Rename method type identifier to function
- Implement match property HOT 1
- Implement system schema HOT 1
- Make fields property required (remove shorthand)
- Document match property HOT 1
- Document local property HOT 1
- Document format property HOT 1
- Create command line interface
- ANN: Discuss v1 release HOT 2
- is support for ruleset X or ruleset Y supported? HOT 5
- Support top-level array with resolve HOT 1
- empty string should be raised when rules include `required` HOT 1
- Override global default error messages HOT 2
- How to validate against other values? HOT 6
- pattern.schema.email only allows TLDs of 6 characters HOT 1
- Wrong behavior with deep rules HOT 5
- after invoke some times, the `type: ['string', 'object']` --> `type: ['string']. HOT 5
- about form data validator ,data type is number or date ,from data validator not through. HOT 1
- 非常严重的问题
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async-validate.