Comments (10)
Good idea, i was thinking of doing this, but i didnt have time. I was going do this for the next release but if you can help ill be realy greatul
🙏
from forgjs.
I will 👍
from forgjs.
I was thinking about the proposition you made and I dont know what behaviour we should get when the error is in multiple keys like so:
const createMinMaxIntRule = (min, max) => {
return new Rule(
{ type: 'integer', min, max },
(key, value) => `${key} must be greater then ${num}, ${value} is not enough`
)
} // whats te expected behaviour in the case where the int doesn't resbect both max and min
from forgjs.
What do you mean?
the error is not related to "min" or "max", it is related to the rule in general?
for now, you can do:
const createMinMaxIntRule = (min, max) => {
return new Rule(
{ type: 'integer', min, max },
(key, value) => `${key} must be greater then ${max} and lower than ${min}.`
)
}
but I guess you are right maybe should be a better solution for this.
from forgjs.
Let's keep the talk here if you dont mind.
I have an other idea which is going and keep this clean and wont break copmpatibility tell me what you think of this:
const vComplexe = new Validator({
age: new Rule({
type: 'int', min: 18, max: 99,
}, 'age must be integer and between 18 and 99'),
dateOfBirth: new Rule({ type: 'date' }, 'date must be a date'),
name: new Rule({ type: 'string', minLength: 2 }, (key, value) => ({
type: `${key} must be a string, ${value} is not a string`,
minLength: `${key} must be at least 2 characters, ${value} is not`,
})),
});
vComplexe.getErrors({
age: 16,
dateOfBirth: new Date(),
name: 'a',
});
I still don't know what should be returned
from forgjs.
do you want that the error will return an object of errors like this:
{
type: 'name must be a string,',
minLength: 'name must be at least 2 chars',
}
or
[ 'name must be a string,', 'name must be at least 2 chars',]
and how it is going to work? if fall on the first error then the other checks are not validating?
and what about the default error, if there is an error and there is no key in the error object?
from forgjs.
I think we should make another function get error in rule that goes trough all the object and returns all the errors for the rule like it’s done for validator. We can make a default error message if none is provided. Do you think this approche is right?
from forgjs.
FIrst how does the API will look like, something like this?
new Rule({
type: 'int', min: 18, max: 99,
}, {
// If the error is object it will run over the object and collect errors if needed
type: 'type error', // you can pass error as string inside the object
min: (key) => `${key} with min error` // you can pass errors in function inside the object
}), // but you will also need to set a default message for all the other properties that does not have message decleration (letting the user chose the default message)
and then we need to think how it will work with the existing code, because now it is just checking each propery and return true or false maybe some function like "testWithErrors" or somthing, that will test and will return array of errors at the end, if the array is empty thats mean that there is no errors
tell me what do you think about it? and do you have idea for the defaukt message?
from forgjs.
That looks good I’ll try doing the first part today i don’t want to break to much code I’ll try my best if you already have an idea on how to do it I’ll be happy to see it
from forgjs.
I will try do something later
from forgjs.
Related Issues (20)
- Type boolean? HOT 4
- Provide example with HTML form HOT 6
- Support for validating Nested Objects HOT 1
- The equal value of rule HOT 3
- allowed fields HOT 9
- Validator.getErrors() HOT 5
- throw errors HOT 1
- example in README fails due to typos HOT 1
- errorMessages is empty array when validation fails due to unexpected field HOT 4
- Duplicate error messages when check for multiple types. HOT 2
- Validator.getErrors() throws an error on undefined fields
- changing the order of the rule properties causes an error
- Validating a null property will cause an error HOT 1
- Validating an undefined property will cause an error HOT 1
- create-react-app + forgJS HOT 11
- # doc and more doc
- Optional property to support "empty" values HOT 1
- Validating an empty string with notEmpty=false returns false HOT 7
- Incorrect error message returned on nested array rules HOT 4
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 forgjs.