TheJaredWilcurt / vue-doxen

The world's best Vue.js component documentation tool
http://TheJaredWilcurt.github.io/vue-doxen
MIT License
6 stars 0 forks source link

Guess "allowed" from validator #15

Closed TheJaredWilcurt closed 1 month ago

TheJaredWilcurt commented 3 months ago

It is a pretty common pattern for props that only allow specific values to have a validator function like this:

{
  myProp: {
    validator: function (value) {
      return ['red', 'blue'].includes(value);
    }
  }
}

We can access the props and validator programmatically with component.props.myProp.validator.

We can convert the validator to a string with '' + fn.

Then we could write some regex to check to see if the validator follows this predictable pattern, and if so extract the part of the string representing the array, then parse it to JSON and use it on the allowed part of the Props Documentation.

The code for this should probably live in the /lib/components/PropsDocumentation.vue file.

TheJaredWilcurt commented 3 months ago

The regex should match against all of these:

const obj = {
  v: function v(value){
    return ['234'].includes(value);
  },
  w(value) {
    return [
      'asd',
      'wer'
    ].includes(value);
  },
  x: function (value) {
    return [123, true, 'yui'].includes(value);
  },
  y: (value) => {
    return ['3gf', 'cf'].includes(value);
  },
  z: v => ['oiu', 'gfd', 'dfg'].includes(v)
};