CREDENTIAL TYPE MANAGEMENT WORKS
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { Collection } from "@discordjs/collection";
|
||||
import { CredentialField, CredentialTypeField } from "./credentialTypeDefs";
|
||||
import GenericError from "../../Errors/GenericError";
|
||||
|
||||
/**
|
||||
* Field Validator
|
||||
*
|
||||
* This method will take a record of the fields being submitted and compare them against a record of the acceptable fields
|
||||
* for a credential type. If any of the submitted fields do not match an acceptable field, an error will be thrown.
|
||||
*
|
||||
* If all the credentials pass, it will return a processed version of the submitted fields including fields that need to be
|
||||
* stored securely (encrypted) and fields that do not.
|
||||
*
|
||||
* @param fields - The fields in object form that are being submitted.
|
||||
* @param acceptableFields - The acceptable field to be compared against.
|
||||
*/
|
||||
export const fieldValidator = async (
|
||||
fields: CredentialField[],
|
||||
acceptableFields: CredentialTypeField[],
|
||||
): Promise<
|
||||
{
|
||||
id: string;
|
||||
fieldId: string;
|
||||
value: string;
|
||||
secure: boolean;
|
||||
}[]
|
||||
> => {
|
||||
const afCollection = new Collection(acceptableFields.map((f) => [f.id, f]));
|
||||
|
||||
await Promise.all(
|
||||
fields.map(async (field) => {
|
||||
const matchingField = afCollection.get(field.fieldId);
|
||||
if (!matchingField) {
|
||||
throw new GenericError({
|
||||
message: `Invalid field ID: ${field.fieldId}`,
|
||||
name: "InvalidCredentialField",
|
||||
cause: `No acceptable field with ID '${field.fieldId}' found.`,
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
}),
|
||||
);
|
||||
|
||||
return fields.map((field) => {
|
||||
const matchingField = afCollection.get(field.fieldId)!;
|
||||
|
||||
return {
|
||||
id: field.id,
|
||||
fieldId: field.fieldId,
|
||||
value: field.value,
|
||||
secure: matchingField.secure,
|
||||
};
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user