All files / src/inspectors checkCredentialSchemaConformity.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 0/0
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                  3x       13x       14x     14x   1x 1x     13x   14x   4x          
import { validator } from '@exodus/schemasafe';
import { request } from '@blockcerts/explorer-lookup';
import { VerifierError } from '../models';
import { SUB_STEPS } from '../domain/verifier/entities/verificationSteps';
import { getText } from '../domain/i18n/useCases';
import type { VCCredentialSchema } from '../models/BlockcertsV3';
 
export default async function checkCredentialSchemaConformity (credentialSubject: any | any[], credentialSchema: VCCredentialSchema | VCCredentialSchema[]): Promise<void> {
  if (!Array.isArray(credentialSchema)) {
    credentialSchema = [credentialSchema];
  }
 
  if (!Array.isArray(credentialSubject)) {
    credentialSubject = [credentialSubject];
  }
 
  for (const schemaInfo of credentialSchema) {
    const rawSchema = await request({ url: schemaInfo.id });
    let schema;
    try {
      schema = JSON.parse(rawSchema);
    } catch (e) {
      console.error(e);
      throw new Error(`Specified schema at url: ${schemaInfo.id} could not be parsed`);
    }
 
    const validate = validator(schema);
    for (const subject of credentialSubject) {
      const result = validate(subject);
      if (!result) {
        throw new VerifierError(SUB_STEPS.checkCredentialSchemaConformity, getText('errors', 'checkCredentialSchemaConformity'));
      }
    }
  }
}