question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question]: Access other model properties in validation function

See original GitHub issue

Is your feature request related to a problem? Please describe. I store my validation rules in separate file. When I’m trying to base validation function on other model property - I’m unable to do it.

Describe the solution you’d like According to doc I was expecting that I should get model as a second parameter. AFAIK I get Vue instance. I’m unable to access other model properties as stated in doc. I’d like use vuelidate as stated in linked doc.

Additional context@vuelidate/core”: “^2.0.0-alpha.25”, “@vuelidate/validators”: “^2.0.0-alpha.21”, “vue”: “^3.2.6”,

Simplified example rules.ts

const requireIfActive = (value: string, vm: any) => {
  // how to access model property active?
  // vm.active is undefined
  return false
}

export default {
  name: {
    required: requireIfActive
  }
}

TheComponent.vue

<script setup lang="ts">
  import rules from './rules'

  const record = ref({
    active: false,
    name: null,
  })

  useVuelidate(rules, record, { $lazy: true })
}

Is there anything I’m doing wrong?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
dobromir-hristovcommented, Oct 3, 2021

I am not sure I get what you need here. As for the forEach helper, I can pass the current iterated object to a validator, as as second parameter. Is that something that you feel would help you? For example:

const items = [
  { name: '', surname: '' }, // this entire object gets passed to each rule as a second parameter, pushing the Instance to third.
  { name: '', surname: '' },
]

const sameAsSurname = (value, surroundingState, instance) => value === surroundingState.surname

In order to help you more, I would need to see code 😕

0reactions
khorskycommented, Nov 27, 2021

@dobromir-hristov OMG - I’m taking back everything what I’ve just written!

Couple of minutes later and it works like a charm! It looks like I get a problem with npm update or so, because now I get the behavior I was looking for.

Sorry for inconvenience.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Custom validation method that accesses other model ...
The validation annotation I'm writing will be applied to the Wheelchair property, and I need to access the TimetableId property from within my ......
Read more >
Restrict data input by using validation rules
Validations rules help you check data as it is added to your Access desktop database which improves accuracy and consistency of data entry....
Read more >
Frequently Asked Questions | Ajv JSON schema validator
Why Ajv assigns errors as a property of validation function (or instance) instead of returning an object with validation results and errors?
Read more >
Model instance reference
To create a new instance of a model, instantiate it like any other Python class: ... or to do validation that requires access...
Read more >
The Complete Guide To Validation In ASP.NET MVC 3 - Part 2
We will include functionality to output HTML5 data-* attributes for use with ... This (among other things) gives us access to the whole...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found