Created
March 3, 2024 12:10
-
-
Save foyzulkarim/bd342263f2af5ce74df90d6540cd59f0 to your computer and use it in GitHub Desktop.
Simplify Express.js Validation: Sync Joi and Mongoose Schemas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const viewModelProps = { | |
title: { type: String, isRequired: true, minLength: 5, unique: true, index: true }, | |
category: { type: String, isRequired: true }, | |
} | |
// in mongoose | |
const videoSchema = new mongoose.Schema({ | |
...generateSchema(true, viewModelProps), | |
duration: { | |
type: Number, | |
min: 0, | |
}}); | |
const Video = mongoose.model('Video', videoSchema); | |
// in joi | |
const schema = Joi.object().keys({ | |
_id: Joi.string().optional(), | |
...generateSchema(false, viewModelProps), | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isJoiString = (options = {}) => { | |
const { minLength = 1, maxLength = 50, required = true } = options; | |
let schemaProp = Joi.string().min(minLength).max(maxLength); | |
if (required) { | |
schemaProp = schemaProp.required(); | |
} | |
return schemaProp; | |
}; | |
const isMongoString = (options = {}) => { | |
const { | |
minLength = 1, | |
maxLength = 100, | |
required = true, | |
trim = true, | |
...otherOptions | |
} = options; | |
return { | |
type: String, | |
required, | |
trim, | |
minLength, | |
maxLength, | |
...otherOptions, | |
}; | |
}; | |
const isString = (isDbSchema, options = {}) => { | |
if (isDbSchema) { | |
return isMongoString(options); | |
} else { | |
return isJoiString(options); | |
} | |
}; | |
const generateSchema = (isDb, props) => { | |
Object.keys(props).forEach((key) => { | |
const propertyOptions = props[key]; | |
if (propertyOptions.type.name === 'String') { | |
schema[key] = isString(isDb, propertyOptions); | |
} else { | |
// Handle unsupported types - you might want to throw an error here | |
throw new Error(`Unsupported type: ${propertyOptions.type}`); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment