23andMe / Yamale

A schema and validator for YAML.
MIT License
679 stars 88 forks source link

Issue validating root keys.. #172

Closed naqoyqatsi83 closed 3 years ago

naqoyqatsi83 commented 3 years ago

First of all.. thank you all for your great job with this project.. I really find it very useful..

Anyway, I run to some issues with usage of it and my problem seems to be similar to this one

I have some yaml files with network definition. Problem is that root key is name of network which I need to validate

192.168.1.0/24:
  default_gw: 192.168.1.254
  dns: 192.168.1.1
  mtu: 1500

192.168.2.0/24:
  default_gw: 192.168.2.254
  dns: 192.168.2.1
  mtu: 9000

and I would like to validate data including root keys.. but.. I've created so far something like:

network: any(include('regular'), include('storage'))
---
regular:
  default_gw: regex('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
  dns: regex('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
  mtu: enum(1500)

storage:
  default_gw: regex('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
  dns: regex('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
  mtu: enum(9000)

and it's failing to pass tests.. :

        192.168.1.0/24: Unexpected element
        192.168.2.0/24: Unexpected element
        network: Required field missing

.. and on top of that.. I would like to validate also name of key, but combination of map and include doesn't seems to be working

is it possible also validate if name of key is valid network name / size

am I missing something or I am trying to use this tool wrong way?

Thanks in advance!

mildebrandt commented 3 years ago

Hi, thanks for your interest in Yamale.

This is really closer to issue https://github.com/23andMe/Yamale/issues/62 .

What your schema is expecting is a root attribute named network. Instead, you want to define a map with some validation on the keys. You can change the top line to something like this:

map(include('regular'), include('storage'), key=regex('^([0-9]{1,3}\.){3}[0-9]{1,3}($|/[0-9]{1,2})$'))

You'll want to change that regex to something a little more robust, that's not my forte. Let me know if you have any other questions.

naqoyqatsi83 commented 3 years ago

Hi,

thank you very much for very quick help!

Your suggested solution works perfectly and also now also documentation makes bit more sense.

Thanks!