|
| 1 | +from typing import Dict |
| 2 | + |
| 3 | +from src.claims import EARClaims |
| 4 | +from src.trust_claims import TrustClaim |
| 5 | +from src.trust_vector import TrustVector |
| 6 | +from src.verifier_id import VerifierID |
| 7 | + |
| 8 | + |
| 9 | +class EARValidationError(Exception): |
| 10 | + """Custom exception for validation errors in EARClaims""" |
| 11 | + |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +def validate_trust_claim(trust_claim: TrustClaim): |
| 16 | + # Validates a TrustClaim object |
| 17 | + if not isinstance(trust_claim.value, int) or not -128 <= trust_claim.value <= 127: |
| 18 | + raise EARValidationError( |
| 19 | + f"Invalid value in TrustClaim: {trust_claim.value}. Must be in range [-128, 127]" |
| 20 | + ) |
| 21 | + if not isinstance(trust_claim.tag, str): |
| 22 | + raise EARValidationError("TrustClaim tag must be a string") |
| 23 | + if not isinstance(trust_claim.short, str): |
| 24 | + raise EARValidationError("TrustClaim short description must be a string") |
| 25 | + if not isinstance(trust_claim.long, str): |
| 26 | + raise EARValidationError("TrustClaim long description must be a string") |
| 27 | + |
| 28 | + |
| 29 | +def validate_trust_vector(trust_vector: TrustVector): |
| 30 | + # Validates a TrustVector object |
| 31 | + if not isinstance(trust_vector, TrustVector): |
| 32 | + raise EARValidationError("Invalid TrustVector object") |
| 33 | + |
| 34 | + for claim in trust_vector.__dict__.values(): |
| 35 | + if claim is not None: |
| 36 | + validate_trust_claim(claim) |
| 37 | + |
| 38 | + |
| 39 | +def validate_verifier_id(verifier_id: VerifierID): |
| 40 | + # Validates a VerifierID object |
| 41 | + if not isinstance(verifier_id, VerifierID): |
| 42 | + raise EARValidationError("Invalid VerifierID object") |
| 43 | + if not verifier_id.developer or not isinstance(verifier_id.developer, str): |
| 44 | + raise EARValidationError("VerifierID developer must be a non-empty string") |
| 45 | + if not verifier_id.build or not isinstance(verifier_id.build, str): |
| 46 | + raise EARValidationError("VerifierID build must be a non-empty string") |
| 47 | + |
| 48 | + |
| 49 | +def validate_ear_claims(ear_claims: EARClaims): |
| 50 | + # Validates an EARClaims object |
| 51 | + if not isinstance(ear_claims, EARClaims): |
| 52 | + raise EARValidationError("Invalid EARClaims object") |
| 53 | + if not isinstance(ear_claims.profile, str) or not ear_claims.profile: |
| 54 | + raise EARValidationError("EARClaims profile must be a non-empty string") |
| 55 | + if not isinstance(ear_claims.issued_at, int) or ear_claims.issued_at <= 0: |
| 56 | + raise EARValidationError("EARClaims issued_at must be a positive integer") |
| 57 | + |
| 58 | + validate_verifier_id(ear_claims.verifier_id) |
| 59 | + |
| 60 | + for submod, details in ear_claims.submods.items(): |
| 61 | + if ( |
| 62 | + not isinstance(details, Dict) |
| 63 | + or "trust_vector" not in details |
| 64 | + or "status" not in details |
| 65 | + ): |
| 66 | + raise EARValidationError( |
| 67 | + f"Submodule {submod} must contain a valid trust_vector and status" |
| 68 | + ) |
| 69 | + validate_trust_vector(details["trust_vector"]) |
| 70 | + if not isinstance(details["status"], str): |
| 71 | + raise EARValidationError(f"Submodule {submod} status must be a string") |
| 72 | + |
| 73 | + |
| 74 | +def validate_all(ear_claims: EARClaims): |
| 75 | + # Runs all validation checks on the provided EARClaims object |
| 76 | + try: |
| 77 | + validate_ear_claims(ear_claims) |
| 78 | + print("EARClaims validation successful.") |
| 79 | + except EARValidationError as e: |
| 80 | + print(f"Validation failed: {e}") |
0 commit comments