Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime validator checks #6

Open
IzaakWN opened this issue Jan 20, 2021 · 1 comment
Open

Runtime validator checks #6

IzaakWN opened this issue Jan 20, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@IzaakWN
Copy link
Contributor

IzaakWN commented Jan 20, 2021

The current pydantic Models are useful to create valid JSON files. One could add some more basic checks during generation, like checking

pydantic provides validator methods, e.g.

class Binning(Model):
    nodetype: Literal["binning"]
    input: str
    edges: List[float]
    "Edges of the binning, where edges[i] <= x < edges[i+1] => f(x, ...) = content[i](...)"
    content: List[Content]
    
    @validator('edges')
    def validate_edges(cls,edges,values):
        for i, lowedge in enumerate(edges[:-1]):
          if lowedge>=edges[i+1]:
            raise ValueError("bin edges not in increasing order: %s"%(edges))
        return edges
    
    @validator('content')
    def match_content_bins(cls,content,values):
        if 'edges' in values:
          nbins = len(values['edges'])-1
          if len(content)!=nbins:
            raise ValueError("number of content elements (%s) must match number of bins (%s)"%(len(content),nbins))
        return content

or one could override __init__.
More examples are here. (pydantic errors are quite verbose, so I added some highlighting.)

@nsmith-
Copy link
Collaborator

nsmith- commented Jan 26, 2021

This is a great addition. If you are willing, feel free to make a PR with the validators you wrote.

@nsmith- nsmith- added the enhancement New feature or request label Feb 16, 2021
lwezenbe referenced this issue in lwezenbe/correctionlib Apr 7, 2021
Rebase & fix bug in JSON encoder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants