@credo-ts/core
Version:
<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1
48 lines (44 loc) • 2.28 kB
JavaScript
import { isJsonObject } from "../../types.mjs";
import { CREDENTIALS_CONTEXT_V1_URL, VERIFIABLE_CREDENTIAL_TYPE, VERIFIABLE_PRESENTATION_TYPE } from "./constants.mjs";
import { ValidateBy, buildMessage, isString, isURL } from "class-validator";
//#region src/modules/vc/validators.ts
function IsCredentialJsonLdContext(validationOptions) {
const allowString = validationOptions?.allowString ?? false;
const credentialContext = validationOptions?.credentialContext ?? CREDENTIALS_CONTEXT_V1_URL;
return ValidateBy({
name: "IsCredentialJsonLdContext",
validator: {
validate: (value) => {
if (!Array.isArray(value)) return allowString && isString(value) && value === credentialContext;
if (value[0] !== credentialContext) return false;
return value.every((v) => isString(v) && isURL(v) || isJsonObject(v));
},
defaultMessage: buildMessage((eachPrefix) => `${eachPrefix}$property must be an array of strings or objects, where the first item is the verifiable credential context URL.`, validationOptions)
}
}, validationOptions);
}
function IsCredentialType(validationOptions) {
return ValidateBy({
name: "IsVerifiableCredentialType",
validator: {
validate: (value) => {
return Array.isArray(value) ? value.includes(VERIFIABLE_CREDENTIAL_TYPE) && value.every((v) => typeof v === "string") : value === VERIFIABLE_CREDENTIAL_TYPE;
},
defaultMessage: buildMessage((eachPrefix) => `${eachPrefix}$property must be "VerifiableCredential" or an array of strings which includes "VerifiableCredential"`, validationOptions)
}
}, validationOptions);
}
function IsVerifiablePresentationType(validationOptions) {
return ValidateBy({
name: "IsVerifiablePresentationType",
validator: {
validate: (value) => {
return Array.isArray(value) ? value.includes(VERIFIABLE_PRESENTATION_TYPE) && value.every((v) => typeof v === "string") : value === VERIFIABLE_PRESENTATION_TYPE;
},
defaultMessage: buildMessage((eachPrefix) => `${eachPrefix}$property must be "VerifiablePresentation" or an array of strings which includes "VerifiablePresentation"`, validationOptions)
}
}, validationOptions);
}
//#endregion
export { IsCredentialJsonLdContext, IsCredentialType, IsVerifiablePresentationType };
//# sourceMappingURL=validators.mjs.map