Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kwalify YAML Validation - Use regexes in key names?

I'm using Kwalify for schema validation. One part of the YAML document actually does want to allow for key names of a certain type in a mapping.

I see that Kwalify support regexes for the values in a mapping, but I see no such mention of support for using regexes in the keys in a mapping. Here's what I'd like to support validating:

test-element:
  sub-element-1: test
  sub-element-2:
    element-with-pattern-1: test1
    element-with-pattern-2: test2

So I don't know what some key names will exactly be in advance (shown here with the fake names "element-with-pattern-*", but I do know that they should correspond to a pattern defined by a regex.

Is this possible to validate using Kwalify?

like image 339
dsw88 Avatar asked Dec 14 '25 03:12

dsw88


2 Answers

To check this:

parent_key:
  random_key1: url1
  random_key2: url2

you should use "Default of Mapping", here is schema example:

type: map
mapping:
  "parent_key":
    type: map
    mapping:
      "=":
        type: str

http://www.kuwata-lab.com/kwalify/ruby/users-guide.02.html#tips-default

like image 71
hraphrap Avatar answered Dec 15 '25 17:12

hraphrap


I do not believe it's possible given the current state of the code.

I'm actually in a similar situation, which I found out (the hard way) does not work well for validation in the Kwalify context. I've begun migrating away from flexible key names into a paradigm where I can specifically define the schema.

For example, I migrated this:

parent_key:
  random_key1: url1
  random_key2: url2

To:

parent_key:
  - name: random_key1
    url: url1 
  - name: random_key2
    url: url2

With the latter syntax, you can validate like this:

"parent_key":
  type: seq
  sequence:
    - type: map
      mapping:
        "name":
          type: str
          required: yes
        "url":
          type: str
          required: yes

Within that context, you can add a pattern regex validators to either name or url that should allow you to achieve your goal.

like image 24
Devin Breen Avatar answered Dec 15 '25 16:12

Devin Breen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!