You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def_validate_string_format(self, val, format):
"""Validates an ATProto string value against a format. https://atproto.com/specs/lexicon#string-formats Args: val (str) format (str): one of the ATProto string formats Raises: ValidationError: if the value is invalid for the given format """defcheck(condition):
ifnotcondition:
raiseValidationError(f'is invalid for format {format}')
check(val)
# TODO: switch to match once we require Python 3.10+ifformat=='at-identifier':
check(DID_RE.match(val) orDOMAIN_RE.match(val.lower()))
elifformat=='at-uri':
check(len(val) <8*1024)
check(AT_URI_RE.match(val))
check('/./'notinvaland'/../'notinvalandnotval.endswith('/.')
andnotval.endswith('/..'))
elifformat=='cid':
# ideally I'd use CID.decode here, but it doesn't support CIDv5,# it's too strict about padding, etc.check(CID_RE.match(val))
elifformat=='datetime':
check('T'inval)
orig_val=val# timezone is requiredval=re.sub(r'([+-][0-9]{2}:[0-9]{2}|Z)$', '', orig_val)
check(val!=orig_val)
# strip fractional secondsval=re.sub(r'\.[0-9]+$', '', val)
try:
datetime.fromisoformat(val)
exceptValueError:
check(False)
elifformat=='did':
check(DID_RE.match(val))
elifformat=='nsid':
check(len(val) <=317)
check(NSID_RE.match(val) and'.'inval)
elifformatin'handle':
check(len(val) <=253)
check(DOMAIN_RE.match(val.lower()))
elifformat=='tid':
check(TID_RE.match(val))
# high bit, big-endian, can't be 1check(notord(val[0]) &0x40)
elifformat=='record-key':
check(valnotin ('.', '..') andRKEY_RE.match(val))
elifformat=='uri':
check(len(val) <8*1024)
check(' 'notinval)
parsed=urlparse(val)
check(parsed.schemeandparsed.scheme[0].lower() instring.ascii_lowercaseand (parsed.netlocorparsed.pathorparsed.queryorparsed.fragment))
elifformat=='language':
check(LANG_RE.match(val))
else:
raiseValidationError(f'unknown format {format}')
the code above is licensed under CC0 1.0 Universal
I see this task as:
implement pydantic validators. 1 per each string format
tune models generator to apply validators for fields
The text was updated successfully, but these errors were encountered:
lexicon`s string type has the "type" field which directs strict string pattern https://atproto.com/specs/lexicon
here is the list of these formats (https://atproto.com/specs/lexicon#string-formats):
We could make our model validation more strict. For someone who wants to use SDK`s models from the server side. To implement PDS, for example.
snarfed implemented this string validations and kindly shared with us: https://github.com/snarfed/lexrpc/blob/41a858c2c28ad212df64f347270c3a8092743f1b/lexrpc/base.py#L439-L522
the code above is licensed under CC0 1.0 Universal
I see this task as:
The text was updated successfully, but these errors were encountered: