yaml-validator icon indicating copy to clipboard operation
yaml-validator copied to clipboard

Prototypes

Open twifty opened this issue 6 years ago • 4 comments

I was so hoping this would fit into my project, but I can't see, either in your readme or tests, if it supports a prototype schema.

Given the following yaml:

app:
  services:
    foo:
        verb: "Give"
        quantifier: 10
        noun: "slaps"
    bar:
        verb: "Accept"
        quantifier: "any"
        noun: "women"

Each service must conform to the same schema, but it's unknown how many there are or what keys they will use.

Looking at the sources, I don't believe it is possible to read the file myself and apply a schema against an existing object. If it were, I could have done a multi stage validation.

twifty avatar May 16 '18 07:05 twifty

How would you see that being configured?

paazmaya avatar May 16 '18 10:05 paazmaya

Just thinking out loud, but you could treat the keys as a regex. Something like:

const options = {
  structure: {
    school: {
      'description?': 'string', //Optional, won't show in invalid array
      code: 'number',
      principal: {
        name: 'string'
      },
      teachers: {
        '/[\w-_]+/': {
           name: 'string',
           subject: 'string',
           salary: 'number',
        }
      }
    }
  }
};

Checking the first and last char for the delimiter should keep things backwards compatible.

twifty avatar May 16 '18 10:05 twifty

That could work, using all keys as regular expressions.

Want to make a PR out of this?

paazmaya avatar May 16 '18 14:05 paazmaya

I'd be willing to build a PR for this, but I think a sanity rule is needed:

-When validating objects, non-RE keys are checked before RE keys

  • RE keys are always optional.
  • If multiple REs match the input, all of their structures must match.

The issue is that we could get something like:

{
  specificKey: 'string',
  "/[A-Za-z_][A-Za-z0-9_]+/":  { ... something else ... }
}

Note also that "/blah/" is actually a valid JSON key that would now need to be matched using a regexp. I don't see that as a problem, but it's definitely something to call out in the documentation.

jsshapiro avatar Mar 21 '21 01:03 jsshapiro