validateId method

ValidationResult validateId(
  1. Credential<Claim> credential,
  2. Claim claim,
  3. String contextPath
)

Implementation

ValidationResult validateId(
  Credential credential,
  Claim claim,
  String contextPath,
) {
  // REQUIRED if claim_sets is present in the Credential Query;
  // OPTIONAL otherwise. A string identifying the particular claim.
  // The value MUST be a non-empty string consisting of alphanumeric, underscore (_), or hyphen (-) characters. Within the particular claims array, the same id MUST NOT be present more than once.
  bool hasClaimSet =
      credential.claimSets != null && credential.claimSets!.isNotEmpty;

  if (claim.id == null && hasClaimSet) {
    return ValidationResult.invalid(
      contextPath: '$contextPath.id',
      errors: [
        'Claim ID is required if "claim_sets" is present in the Credential.',
      ],
    );
  }

  if (claim.id != null) {
    if (claim.id!.isEmpty) {
      return ValidationResult.invalid(
        contextPath: '$contextPath.id',
        errors: ['Claim ID cannot be empty if provided.'],
      );
    }

    final idPattern = RegExp(r'^[a-zA-Z0-9_-]+$');
    if (!idPattern.hasMatch(claim.id!)) {
      return ValidationResult.invalid(
        contextPath: '$contextPath.id',
        errors: [
          'Claim ID must consist of alphanumeric, underscore (_), or hyphen (-) characters.',
        ],
      );
    }
  }

  return ValidationResult.valid();
}