How to use the jsonld.getValues function in jsonld

To help you get started, we’ve selected a few jsonld examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github digitalbazaar / vc-js / lib / vc.js View on Github external
if(!Array.isArray(credential['type']) ||
      credential['type'].length < 2 ||
      !credential['type'].includes('VerifiableCredential')) {
    throw new Error('"type" must be `VerifiableCredential` plus specific type.');
  }

  if(!credential['credentialSubject']) {
    throw new Error('"credentialSubject" property is required.');
  }

  if(!credential['issuer']) {
    throw new Error('"issuer" property is required.');
  }

  // check issuanceDate cardinality
  if(jsonld.getValues(credential, 'issuanceDate').length > 1) {
    throw new Error('"issuanceDate" property can only have one value.');
  }

  // check issued is a date
  if(!credential['issuanceDate']) {
    throw new Error('"issuanceDate" property is required.');
  }

  if('issuanceDate' in credential) {
    if(!dateRegex.test(credential.issuanceDate)) {
      throw new Error(`"issuanceDate" must be a valid date: ${credential.issuanceDate}`);
    }
  }

  // check issuer cardinality
  if(jsonld.getValues(credential, 'issuer').length > 1) {
github digitalbazaar / vc-js / lib / vc.js View on Github external
function _checkCredential(credential) {
  // check context cardinality
  if(jsonld.getValues(credential, '@context').length < 2) {
    throw new Error(
      '"@context" property needs to be an array of two or more contexts.');
  }

  // ensure first context is 'https://www.w3.org/2018/credentials/v1'
  if(credential['@context'][0] !== 'https://www.w3.org/2018/credentials/v1') {
    throw new Error(
      'https://www.w3.org/2018/credentials/v1 needs to be first in the list of contexts.');
  }

  // check type presence and cardinality
  if(!credential['type']) {
    throw new Error('"type" property is required.');
  }

  if(!Array.isArray(credential['type']) ||
github digitalbazaar / vc-js / lib / vc.js View on Github external
function _checkPresentation(presentation) {
  // check context cardinality
  if(jsonld.getValues(presentation, '@context').length < 2) {
    throw new Error(
      '"@context" property needs to be an array of two or more contexts.');
  }

  // ensure first context is 'https://www.w3.org/2018/credentials/v1'
  if(presentation['@context'][0] !== 'https://www.w3.org/2018/credentials/v1') {
    throw new Error(
      'https://www.w3.org/2018/credentials/v1 needs to be first in the list of contexts.');
  }

  // check type presence and cardinality
  if(!presentation['type']) {
    throw new Error('"type" property is required.');
  }

  if(!Array.isArray(presentation['type']) ||
github digitalbazaar / jsonld-signatures / lib / ProofSet.js View on Github external
async function _getTypeInfo({document, documentLoader, expansionMap}) {
  // determine `@type` alias, if any
  const ctx = jsonld.getValues(document, '@context');
  const compacted = await jsonld.compact(
    {'@type': '_:b0'}, ctx, {documentLoader, expansionMap});
  delete compacted['@context'];
  const alias = Object.keys(compacted)[0];

  // optimize: expand only `@type` and `type` values
  const toExpand = {'@context': ctx};
  toExpand['@type'] = jsonld.getValues(document, '@type')
    .concat(jsonld.getValues(document, alias));
  const expanded = (await jsonld.expand(
    toExpand, {documentLoader, expansionMap}))[0] || {};
  return {types: jsonld.getValues(expanded, '@type'), alias};
}
github digitalbazaar / jsonld-signatures / lib / ProofSet.js View on Github external
// compact proof to match document's context
      let expandedProof;
      if(suite.legacy) {
        expandedProof = {
          [constants.SECURITY_SIGNATURE_URL]: proof
        };
      } else {
        expandedProof = {
          [constants.SECURITY_PROOF_URL]: {'@graph': proof}
        };
      }
      // account for type-scoped `proof` definition by getting document types
      const {types, alias} = await _getTypeInfo(
        {document, documentLoader, expansionMap});
      expandedProof['@type'] = types;
      const ctx = jsonld.getValues(document, '@context');
      const compactProof = await jsonld.compact(
        expandedProof, ctx,
        {documentLoader, expansionMap, compactToRelative: false});
      delete compactProof[alias];
      delete compactProof['@context'];

      // add proof to document
      const key = Object.keys(compactProof)[0];
      jsonld.addValue(document, key, compactProof[key]);
    } else {
      // in-place restore any existing proofs
      /*if(existingProofs) {
        document[proofProperty] = existingProofs;
      }*/
      // add new proof
      delete proof['@context'];
github digitalbazaar / did-cli / lib / drivers / veres / helpers.js View on Github external
api._hasKeyId = (didDocument, id) => {
  // check all suites
  for(const suiteId of api._suiteIds) {
    for(const suiteParams of jsonld.getValues(didDocument, suiteId)) {
      for(const key of jsonld.getValues(suiteParams, 'publicKey')) {
        if(key.id === id) {
          return true;
        }
      }
    }
  }
  return false;
};