-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optional validation of input doc
Signed-off-by: Daniel Bluhm <[email protected]>
- Loading branch information
Showing
7 changed files
with
295 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""Validate input documents.""" | ||
from typing import Any, Mapping | ||
|
||
|
||
def resources(document: Mapping[str, Any]): | ||
"""Yield all resources in a document, skipping references.""" | ||
keys = ( | ||
"verificationMethod", | ||
"authentication", | ||
"assertionMethod", | ||
"keyAgreement", | ||
"capabilityDelegation", | ||
"capabilityInvocation", | ||
"service", | ||
) | ||
for key in keys: | ||
if key in document: | ||
if not isinstance(document[key], list): | ||
raise ValueError(f"{key} must be a list") | ||
|
||
for index, resource in enumerate(document[key]): | ||
if isinstance(resource, dict): | ||
yield key, index, resource | ||
|
||
|
||
def validate_input_document(document: Mapping[str, Any]) -> Mapping[str, Any]: | ||
"""Validate did:peer:4 input document. | ||
This validation is deliberately superficial. It is intended to catch mistakes | ||
in the input document that would cause the peer DID to be invalid. It is not | ||
intended to validate the contents of the document, which is left to the caller | ||
after resolution. | ||
The following checks are performed: | ||
- The document must be a Mapping. | ||
- The document must not be empty. | ||
- The document must not contain an id. | ||
- If present, alsoKnownAs must be a list. | ||
- verificationMethod, authentication, assertionMethod, keyAgreement, | ||
capabilityDelegation, capabilityInvocation, and service must be lists, if | ||
present. | ||
- All resources (verification methods, embedded verification methods, | ||
services) must have an id. | ||
- All resource ids must be strings. | ||
- All resource ids must be relative. | ||
- All resources must have a type. | ||
""" | ||
if not isinstance(document, Mapping): | ||
raise ValueError("document must be a Mapping") | ||
|
||
if not document: | ||
raise ValueError("document must not be empty") | ||
|
||
if "id" in document: | ||
raise ValueError("id must not be present in input document") | ||
|
||
if "alsoKnownAs" in document: | ||
if not isinstance(document["alsoKnownAs"], list): | ||
raise ValueError("alsoKnownAs must be a list") | ||
|
||
for key, index, resource in resources(document): | ||
if "id" not in resource: | ||
raise ValueError(f"{key}[{index}]: resource must have an id") | ||
|
||
ident = resource["id"] | ||
if not isinstance(ident, str): | ||
raise ValueError("r{key}[{index}]: esource id must be a string") | ||
|
||
if not ident.startswith("#"): | ||
raise ValueError( | ||
"A{key}[{index}]: ll document resource ids must be relative" | ||
) | ||
|
||
if "type" not in resource: | ||
raise ValueError("r{key}[{index}]: esource must have a type") | ||
|
||
return document |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Tests for did:peer:4.""" | ||
from pathlib import Path | ||
|
||
|
||
def examples(): | ||
"""Load json from examples directory and return generator over examples.""" | ||
examples_dir = Path(__file__).parent / "examples" | ||
yield from examples_dir.glob("*.json") | ||
|
||
|
||
EXAMPLES = list(examples()) |
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
Oops, something went wrong.