-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
field validation based on another field condition #13
Comments
This feature would bring a better way to validate some business rules in a simpler way. In short I liked it. |
I think we should focus on the issue #2 first and come back to this one later since it could be a evolution of it |
Its a more declarative way to write, and opens more possibilities when designing entities validations. Would be nice the possibility to, combined with #2 set different custom validations to the same field based on entity type. Something like: const User = entity("Person", {
name: field(String),
hasChildren: field(Boolean),
hasParents: field(Boolean),
childrenNames: field([String])
.when("hasChildren", {
is: true, // possibility to use a function instead: (val) => val == true
then: { validations: [function hasSameLastName() {}, function checkDNA() {}] }
})
.when("hasParents", {
is: true,
then: { validations: [function hasAllowance() {}, function hasHeritage() {}] }
})
}); or const User = entity("Person", {
name: field(String),
occupation: field(String, {
validation: {
presence: false,
length: { minimum: 1 },
contains: ["Medic", "Lawyer"],
},
}),
exertsGraduation: field(Boolean)
.when("occupation", {
is: (occupation) => occupation === 'Medic',
then: { validations: [function hasCRM() {}] }
})
.when("occupation", {
is: (occupation) => occupation === 'Lawyer',
then: { validations: [function hasBarAssociation() {}] }
})
}); The question is if this validations should be on subtypes entities (different files/entities) by default. |
@thgpdr , just to make sure, is the code below a equivalent of the code proposed? (btw, the code below does not work, it is just a concept) The idea here is to (1) make it clear the intention of the proposed solution and (2) be open for different ideas to get to the same result. const User = entity("Person", {
name: field(String),
occupation: field(String, {
validation: {
presence: false,
length: { minimum: 1 },
contains: ["Medic", "Lawyer"],
},
}),
exertsGraduation: field(Boolean, {
validation: {
custom: {
validOccupation: (value, person) => {
const occupation = person.occupation
if (occupation === 'Medic') return hasCRM()
if (occupation === 'Lawyer') return hasBarAssociation()
}
}
}
})
}); Or even: const User = entity("Person", {
name: field(String),
occupation: field(String, {
validation: {
presence: false,
length: { minimum: 1 },
contains: ["Medic", "Lawyer"],
},
}),
exertsGraduation: field(Boolean, {
validation: {
custom: {
validOccupation: (value, person) => person.hasGraduation()
}
}
}),
hasGraduation() {
if (this.occupation === 'Medic') return hasCRM()
if (this.occupation === 'Lawyer') return hasBarAssociation()
}
}); If the idea was to populate const User = entity("Person", {
name: field(String),
occupation: field(String, {
validation: {
presence: false,
length: { minimum: 1 },
contains: ["Medic", "Lawyer"],
},
onChange: (person) => person.hasGraduation() // triggered by the changed value
}),
exertsGraduation: field(Boolean),
hasGraduation() {
if (this.occupation === 'Medic') this.exertsGraduation = hasCRM()
if (this.occupation === 'Lawyer') this.exertsGraduation = hasBarAssociation()
}
}); @thgpdr please, bring your thoughts to this. thanks! |
const User = entity("Person", {
name: field(String),
occupation: field(String, {
validation: {
presence: false,
length: { minimum: 1 },
contains: ["Medic", "Lawyer"],
},
}),
exertsGraduation: field(Boolean, {
validation: {
custom: {
validOccupation: (value, person) => {
const occupation = person.occupation
if (occupation === 'Medic') return hasCRM()
if (occupation === 'Lawyer') return hasBarAssociation()
}
}
}
})
}); @dalssoft this one suits for me |
If |
Just an example of
a better one would be: const User = entity("Person", {
name: field(String),
occupation: field(String, {
validation: {
presence: false,
length: { minimum: 1 },
contains: ["Coder", "Lawyer", "Doctor"],
},
}),
degree: field(String, {
validation: {
presence: false,
length: { minimum: 1 },
contains: ["Tecnology", "Medicine", "LawSchool"],
},
}),
exertsGraduation: field(Boolean, {
validation: {
custom: {
validOccupation: (value, person) => { //true if the occupation matchs the degree
const degree = person.degree
const occupation = person.occupation
if (degree === 'Medicine' && occupation === 'Doctor') return hasCRM()
if (degree === 'LawSchool' && occupation === 'Lawyer') return hasBarAssociation()
if (degree === 'Tecnology' && occupation === 'Coder') return hasHerbsPr()
}
}
}
})
}); In this example you can observe 2 points: (1) different validations based on person type. (2) the possibility to store some info (exertsGraduation: Boolean) without the need to invalidate occupation input. So this way, I can have a Person who's a coder but has a degree in climatology and store/validate the match (occupation x degree) information in one field. Does that make sense for a real world scenario? |
Given you example, what would happen when |
It should be possible to validate sinbling fields based on a condition.
Something like this:
inspired on yup .when()
what do you guys think?
The text was updated successfully, but these errors were encountered: