23andMe / Yamale

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

better error message for list of different type #210

Open XiaosongWen opened 1 year ago

XiaosongWen commented 1 year ago

My use case I have two type type of target_discovery (http and ping), each of them have different schema, my code is:

target_discovery: list(include('resourcePingProvider'), include('resourceHttpProvider'))

resourcePingProvider:
  resourceProvider: str(equals="ping")
  probeMetadata: include('probeMetadata')
  ...

resourceHttpProvider:
  resourceProvider: str(equals="http")
  url: ...

Basically, it is checking the value of each resourceProvider field. However, if I input "xxxx", it gave me blow error messages, it contains other redundant infos.

target_discovery.0.resourceProvider: XXXX does not equal ping
target_discovery.0.probeMetadata: Unexpected element
... ...
target_discovery.0.resourceProvider: XXXX does not equal http
target_discovery.0.url: Required field missing
... ... 

Is there a way that it only return something like XXXX does not equal ping or http?

nbaju1 commented 1 year ago

You could look into generalizing the resourceProvider schema to handle both types and do:

target_discovery: list(include('resourceProvider'))

resourceProvider:
  resourceProviderType: enum("ping", "http")
  ...

Or you could create a custom constraint that replaces equals which outputs your desired error message.

XiaosongWen commented 1 year ago

@nbaju1 Thanks for your advice. Since different Provider have very different schema, first approach probably wont work. I will try your second suggestion