UNPKG

@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

40 lines (39 loc) • 1.61 kB
import { JsonTransformer } from "../../../../utils/JsonTransformer.mjs"; import { VerificationMethod } from "./VerificationMethod.mjs"; import { Transform, TransformationType } from "class-transformer"; import { ValidateBy, buildMessage, isInstance, isString } from "class-validator"; //#region src/modules/dids/domain/verificationMethod/VerificationMethodTransformer.ts /** * Checks if a given value is a real string. */ function IsStringOrVerificationMethod(validationOptions) { return ValidateBy({ name: "isStringOrVerificationMethod", validator: { validate: (value) => isString(value) || isInstance(value, VerificationMethod), defaultMessage: buildMessage((eachPrefix) => `${eachPrefix}$property must be a string or instance of VerificationMethod`, validationOptions) } }, validationOptions); } /** * Decorator that transforms authentication json to corresponding class instances * * @example * class Example { * VerificationMethodTransformer() * private authentication: VerificationMethod * } */ function VerificationMethodTransformer() { return Transform(({ value, type }) => { if (type === TransformationType.PLAIN_TO_CLASS) return value?.map((auth) => { if (typeof auth === "string") return String(auth); return JsonTransformer.fromJSON(auth, VerificationMethod); }); if (type === TransformationType.CLASS_TO_PLAIN) return value?.map((auth) => typeof auth === "string" ? auth : JsonTransformer.toJSON(auth)); return value; }); } //#endregion export { IsStringOrVerificationMethod, VerificationMethodTransformer }; //# sourceMappingURL=VerificationMethodTransformer.mjs.map