https://github.com/saucecodee/joi-objectid-validator
MongoDB ObjectId validator for Joi
https://github.com/saucecodee/joi-objectid-validator
joi mongodb-objectid objectid validator
Last synced: 6 months ago
JSON representation
MongoDB ObjectId validator for Joi
- Host: GitHub
- URL: https://github.com/saucecodee/joi-objectid-validator
- Owner: saucecodee
- License: mit
- Created: 2021-03-17T22:58:32.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-17T06:04:20.000Z (about 4 years ago)
- Last Synced: 2025-04-24T00:44:36.072Z (6 months ago)
- Topics: joi, mongodb-objectid, objectid, validator
- Language: TypeScript
- Homepage:
- Size: 125 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Joi Objectid validator
MongoDB ObjectId validator for Joi.
### Installation
```
npm install joi-objectid-validator --save
```## Usage
The `joi-objectid-validator` validates only valid MongoDB ObjectId so it accepts either a 24 character hexadecimal String or an ObjectId type
```js
const Joi = require('@hapi/joi')
// Add joi-objectid-validator to Joi object
Joi.objectId = require('joi-objectid-validator')(Joi)const schema = Joi.object({
id: Joi.objectId("Custom error message here"),
})// VALID values
//
// new ObjectID()
// new ObjectID("ccd563780134d5678901234a")
// '6053a49e634e0ec405295ba8'// INVALID values
//
// '$bcd56778c9a345678901234'
// ' bcd56789012345678901234'
// '6053a49e634e0ec405g95ba8'
// 748397383836789234567823
// { }
// null
```### Development
#### Testing
- `npm test`
## License
[MIT](https://github.com/saucecodee/joi-objectid-validator/blob/main/LICENSE)