Created
July 6, 2023 12:11
-
-
Save akotlar/555f8d00cc86b4f830826d484963bcee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from msgspec import json, Struct, Meta | |
from typing import Annotated | |
from msgspec import Struct, Meta | |
import re | |
_VCF_REGEX = ".*\.vcf(\.(gz|lz4|zstd|bzip2))?$" | |
class AncestrySubmission(Struct): | |
vcf_path: Annotated[str, Meta(pattern=_VCF_REGEX)] | |
test_json = '{"vcf_path": "test.vcf"}' | |
json.decode(test_json) | |
# Out[15]: {'vcf_path': 'test.vcf'} | |
json.decode(test_json, type=AncestrySubmission) | |
# Out[19]: AncestrySubmission(vcf_path='test.vcf') | |
bad_test_json = '{"vcf_path": "test.blah"}' | |
json.decode(bad_test_json, type=AncestrySubmission) | |
# --------------------------------------------------------------------------- | |
# ValidationError Traceback (most recent call last) | |
# Cell In[21], line 1 | |
# ----> 1 json.decode(bad_test_json, type=AncestrySubmission) | |
# ValidationError: Expected `str` matching regex '.*\\.vcf(\\.(gz|lz4|zstd|bzip2))?$' - at `$.vcf_path` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment