romaricdrigon / MetaYaml

A powerful schema validator!
MIT License
104 stars 21 forks source link

Is it possible to validate that _type is a yaml sequence? #11

Closed psynaptic closed 6 years ago

psynaptic commented 8 years ago

It would be highly yaml loader specific, so I'm not sure it belongs in core, but we have the need to validate that a structure is a yaml sequence. Is this possible, and if so, how would I generally go about it?

romaricdrigon commented 8 years ago

Hello,

To be sure, you want to check than some output is YAML, isn't it? Do you have an example?

psynaptic commented 8 years ago

Thank you for the amazingly fast response!

I have a YAML document that has "sequences" in it, for example:

foo:
  - ordered_element_1
  - ordered_element_2
  - ordered_element_3
      type: bar
      baz:
        - ordered_element_1
        - ordered_element_2

I think what these are as per the spec are "Ordered Mappings". They ensure the order of elements which is essential to parts of our schema.

Does this feature already exist or can I add it somehow, perhaps in user land code?

romaricdrigon commented 8 years ago

I'm not sure to totally get you yet. You want to validate that ordered_element_1-2-3 are exactly, respectively, first, second and third items? And not something else?

I don't have my computer here to try, I would try to put an "array" node - those are afficheur arrays in PHP terms, an array with unordered elements is an prototype in Metayaml - whose subelements are named 0, 1 and 2.

psynaptic commented 8 years ago

Maybe I can give you an example as I'm struggling to build a schema for my document:

keys: # always called "keys"
  my-key: # any arbitrary key name(s)
    secure: encoded-string # "secure" must always be present in this node and contains a literal string value 
variables: # always called "variables"
  global: # one of a fixed set of 2 choices
    FOO: foo # any arbitrary key name(s) with literal string value
    BAR: bar
  script: # the other of the 2 choices in the set
    foo: foo
    password:
      secure: encoded-string # if the node is an array, it must have a "secure" element
my-steps: # either "my-steps" or "my_steps"
  - step-1: # arbitrary key name. must be an ordered mapping
      type: script # optionally specified type key. if not specified, defaults to "script"
      script: # only if type is "script" or not present should script key be allowed
        - command1 # must be an ordered mapping of literal string values
        - command2
  - step-2:
      type: make # type may be one of a finite set
      file: project.make # if type is "make", the "file" key is required
  - arbitrary-name:
      script: # same as step-1 but omitting type, since the default is "script"
        - echo foo
        - command2

I'm not sure it is even possible to create a MetaYaml schema to describe and validate this, since it is not strict about key names. There are a couple of places where keys are arbitrary but it matters what they contain.

What do you think? Can MetaYaml help with this, or is my document format too loose?

I could write a custom validator for this but I wanted to formalize the data structure requirements by using a schema. I have 2 separate applications that need to validate the document (server and client) and I was hoping to centralize the validation using a schema validator. I'm using PHP and this looks like a good tool for the job, but perhaps it doesn't work well for my specific needs.

romaricdrigon commented 8 years ago

I believe it can work. First of all, wrote a schema for your document without any ambiguity about keys names. After, add some "choice" node to account for the possible variations.

youknowriad commented 8 years ago

@psynaptic for the nodes who have childs with arbitrary key names, you can use the type "prototype" :)

psynaptic commented 8 years ago

That's a good idea @romaricdrigon, I will try that.

@youknowriad: I think the issue I'm going to have though, is that if I want arbitrary keys then I'll lose ordering. It is important for our schema to allow arbitrary keys for ordered mappings. See step-1, step-2 and arbitrary-name in the example, they need to be in the order they are defined but the user can call them anything meaningful to them.