Skip to content

Instantly share code, notes, and snippets.

@akotlar
Created July 6, 2023 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akotlar/555f8d00cc86b4f830826d484963bcee to your computer and use it in GitHub Desktop.
Save akotlar/555f8d00cc86b4f830826d484963bcee to your computer and use it in GitHub Desktop.
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