Giter VIP home page Giter VIP logo

Comments (10)

oussamahamdaoui avatar oussamahamdaoui commented on May 13, 2024

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.

Nevoss avatar Nevoss commented on May 13, 2024

I will 👍

from forgjs.

oussamahamdaoui avatar oussamahamdaoui commented on May 13, 2024

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.

Nevoss avatar Nevoss commented on May 13, 2024

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.

oussamahamdaoui avatar oussamahamdaoui commented on May 13, 2024

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.

Nevoss avatar Nevoss commented on May 13, 2024

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.

oussamahamdaoui avatar oussamahamdaoui commented on May 13, 2024

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.

Nevoss avatar Nevoss commented on May 13, 2024

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.

oussamahamdaoui avatar oussamahamdaoui commented on May 13, 2024

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.

Nevoss avatar Nevoss commented on May 13, 2024

I will try do something later

from forgjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.