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.

Validation attributes

See original GitHub issue

Would like to see something to be able to generate validation attributes among with model classes. It would be really cool.

My view model looks like this: image

I got my api like this: image

I am thinking about to implement client validation using vuelidate or VeeValidate

Is there any way to somehow translate model attributes using swagger-axios-codegen and use them in validation? Any ideas?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fairkingcommented, Oct 4, 2019

Maybe it could be something like this?

ISwaggerOptions = {
  ...
  renderValidationModel: [ true | false ],
  ...
}

codegen output:

export class RoleVm {
  name?: string;
  description?: string;
  static ValidationModel() {
    return {
      name: {
        required,
        minLength: 1,
        maxLength: 50
      },
      description: {
        maxLength: 150
      }
    }
  }
}

It is interesting how easy we can bind the validation model with the vuelidate or VeeValidate.

vuelidate example (not sure it will work):

import { required, minLength, maxLength } from 'vuelidate/lib/validators'

export default {
  data () {
    return {
      model: new RoleVm(),
    }
  },
  validations: RoleVm.ValidationModel()
}

vee-validate example (not sure it will work):

<template>
<validation-provider rules="validations.name" v-slot="{ errors }">
  <input v-model="model.name" name="name" type="text" />
  <span>{{ errors[0] }}</span>
</validation-provider>
</template>

<script>
import { ValidationProvider, extend } from 'vee-validate';
import { required, minLength, maxLength } from 'vee-validate/dist/rules';

extend('required', { ...required, message: 'The {_field_} field is required' });
extend('minLength', { ...minLength, message: 'The {_field_} field must not be less than {_min_} characters' });
extend('maxLength', { ...maxLength, message: 'The {_field_} field must not be more than {_max_} characters' });

new Vue({
  el: '#app',
  components: {
    ValidationProvider
  },
  data: () => ({
    model: new RoleVm(),
    validations: RoleVm.ValidationModel()
  }),
});
<script>
0reactions
fairkingcommented, Mar 23, 2020

Yes you can close. The most important things are covered. If someone needs more validation he/she can raise a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValidationAttribute Class (System.ComponentModel ...
This class enforces validation, based on the metadata that is associated with the data table. You can override this class to create custom...
Read more >
Validation Attributes | ASP.NET Core Controls
Validation Attributes ; Compare, Validates that two properties in a model match. Built-in ; Range, Validates that a property value falls within a ......
Read more >
Client-side form validation - Learn web development | MDN
This is done by using validation attributes on form elements. We've seen many of these earlier in the course, but to recap:.
Read more >
Validation Attributes - powershell.one
Validation attributes monitor and make sure values assigned to variables and parameters meet your requirements. And they can do plenty more!
Read more >
Data Validation in ASP.NET MVC - TutorialsTeacher
Validation using Data Annotation Attributes ; StringLength, Specifies the minimum and maximum length of characters that are allowed in a string type property....
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