Separated rules and dependent values - object validation #201
-
|
Hi, I'm considering to replace vuelidate in my projects with regle and get an assumption that I'm not able to replace my current rules setup yet. I'm separating rule definitions to external files and I often use dependable rules. Pseudocode in MyRules.ts interface MyRecord {
subscription: boolean,
from: Date | null,
to: Date | null
}
export default {
to: {
required: (value: Date | null, record: MyRecord) => {
// require "to" only for subscriptions
// "to" should be greater than "from"
},
}MyComponent.vue ...
const r$ = useRegle(currentState, MyRules)
...The only way I find with regle is to put the rules into the components, because there's not a way how to handle "related" object properties. Can you help me to confirm my assumption? I've read related discussions and issues so I guess that I'm not able to apply my code style with regle yet. Is there any plan to support such a behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
In Regle rules have to be independant to be reusable, if they can access sibling properties there is no way to have a correct type safe way to do it. In your case it's simple to have a workaround. 1Use dateBefore rule that already handles this 2Make your export function MyRules(state: MyRecord) {
return {
to: {
required: (value: Date | null) => {
// require "to" only for subscriptions
// "to" should be greater than "from"
},
}
}const {r$) = useRegle(currentState, () => MyRules(currentState.value)) |
Beta Was this translation helpful? Give feedback.
In Regle rules have to be independant to be reusable, if they can access sibling properties there is no way to have a correct type safe way to do it. In your case it's simple to have a workaround.
1
Use dateBefore rule that already handles this
2
Make your
MyRulesreactive