Using Joi schema validation, is it possible to validate against MongoDB ObjectID's?
Something like this could be great:
_id: Joi.ObjectId().required().error(errorParser),
ObjectId values are 12 bytes in length, consisting of: a 4-byte timestamp value, representing the ObjectId's creation, measured in seconds since the Unix epoch. a 5-byte random value generated once per process. This random value is unique to the machine and process.
No, an ObjectId field that's defined in your schema as a reference to another collection is not checked as existing in the referenced collection on a save.
ObjectID is automatically generated by the database drivers, and will be assigned to the _id field of each document. ObjectID can be considered globally unique for all practical purposes. ObjectID encodes the timestamp of its creation time, which may be used for queries or to sort by creation time.
Binary JSON (BSON)
joi-objectid validates that the value is an alphanumeric string of 24 characters in length. MongoDB ObjectId validator for Joi. new RegExp ("^ [0-9a-fA-F] {23}$").test ("5e79d319ab5bfb2a9ea4239")
Mongoose & MongoDB provide a very useful function in ObjectId i.e. ObjectId.isValid (“some_id”) to validate a string for correct MongoDB ID. ObjectId can be imported from native mongodb as well as mongoose package. const ObjectId = require ('mongodb').ObjectId; or const mongodb, {ObjectId} = require ('mongodb');
OBJECT ID (Transact-SQL) Returns the database object identification number of a schema-scoped object. Objects that are not schema-scoped, such as DDL triggers, cannot be queried by using OBJECT_ID. For objects that are not found in the sys.objects catalog view, obtain the object identification numbers by querying the appropriate catalog view.
"zzzzzzzzzzzz" is not valid ObjectId. For example Mongo shell listiong (mongodb version - 3.0.2): Sorry, something went wrong. Because the mongodb shell's ObjectId constructor is written such that it only accepts hex strings. It's a restriction in the mongo shell for convenience, not with the BSON type ObjectId.
I find that if I do
Joi.object({
id: Joi.string().hex().length(24)
})
it works without installing any external library or using RegEx
The hex
makes sure the string contains only hexadecimal characters and the length
makes sure that it is a string of exactly 24 characters
const Joi = require('@hapi/joi')
Joi.objectId = require('joi-objectid')(Joi)
const schema = Joi.object({
id: Joi.objectId(),
name: Joi.string().max(100),
date: Joi.date()
})
checkout https://www.npmjs.com/package/joi-objectid
If you want a TypeScript version of the above library integrated with Express without installing anything:
import Joi from '@hapi/joi';
import { createValidator } from 'express-joi-validation';
export const JoiObjectId = (message = 'valid id') => Joi.string().regex(/^[0-9a-fA-F]{24}$/, message)
const validator = createValidator({
passError: true,
});
const params = Joi.object({
id: JoiObjectId().required(),
});
router.get<{ id: string }>(
'/:id',
validator.params(params),
(req, res, next) => {
const { id } = req.params; // id has string type
....
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With