Changed Schemas type with a more detailed type Schema

This commit is contained in:
Alexander Cerutti
2019-12-05 23:44:22 +01:00
parent 98dbbac46e
commit 2e94307723

View File

@@ -441,10 +441,7 @@ const personalizationDict = Joi.object().keys({
// --------- UTILITIES ---------- // // --------- UTILITIES ---------- //
type Schemas = { const schemas = {
[index: string]: Joi.ObjectSchema | Joi.StringSchema;
};
const schemas: Schemas = {
instance, instance,
certificatesSchema, certificatesSchema,
barcode, barcode,
@@ -458,7 +455,9 @@ const schemas: Schemas = {
personalizationDict personalizationDict
}; };
function resolveSchemaName(name: keyof Schemas) { type Schema = keyof typeof schemas;
function resolveSchemaName(name: Schema) {
return schemas[name] || undefined; return schemas[name] || undefined;
} }
@@ -469,7 +468,7 @@ function resolveSchemaName(name: keyof Schemas) {
* @returns {boolean} - result of the check * @returns {boolean} - result of the check
*/ */
export function isValid(opts: any, schemaName: keyof Schemas): boolean { export function isValid(opts: any, schemaName: Schema): boolean {
const resolvedSchema = resolveSchemaName(schemaName); const resolvedSchema = resolveSchemaName(schemaName);
if (!resolvedSchema) { if (!resolvedSchema) {
@@ -493,7 +492,7 @@ export function isValid(opts: any, schemaName: keyof Schemas): boolean {
* @returns {object} the filtered value or empty object * @returns {object} the filtered value or empty object
*/ */
export function getValidated<T extends Object>(opts: any, schemaName: keyof Schemas): T { export function getValidated<T extends Object>(opts: any, schemaName: Schema): T {
let resolvedSchema = resolveSchemaName(schemaName); let resolvedSchema = resolveSchemaName(schemaName);
let validation = Joi.validate(opts, resolvedSchema, { stripUnknown: true }); let validation = Joi.validate(opts, resolvedSchema, { stripUnknown: true });