paazmaya / yaml-validator

Validate Yaml files and enforce a given structure
MIT License
56 stars 17 forks source link

Prototypes #17

Open twifty opened 6 years ago

twifty commented 6 years ago

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.

paazmaya commented 6 years ago

How would you see that being configured?

twifty commented 6 years ago

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.

paazmaya commented 6 years ago

That could work, using all keys as regular expressions.

Want to make a PR out of this?

jsshapiro commented 3 years ago

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

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.