Skip to content

Commit

Permalink
Initial commit: setup project initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
kavingovind committed Dec 6, 2024
0 parents commit 9c2a8b0
Show file tree
Hide file tree
Showing 10 changed files with 6,328 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Dependency directories
node_modules/

# Ignore the dist folder (output directory)
dist/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# CHANGELOG.md
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 kavingovind

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# joi-mongo-objectid

A Simple Joi extension to validate and parse MongoDB ObjectId strings.

## What

Allows you to do `Joi.mongo().objectId()`.

## Installation

```bash
$ npm install joi-mongo-objectid --save

or

$ yarn add joi-mongo-objectid
```

## Requisites

```npm
"joi": ">=17.0.0",
"mongoose": ">=6.9.0"
```

## Usage

```javascript
const JoiBase = require("joi");
const { JoiObjectId } = require("joi-mongo-objectid");

const Joi = JoiBase.extend(JoiObjectId);

const schema = Joi.object({
_id: Joi.mongo().objectId().required(),
});

const data = { _id: "67531b0516e6712d5cf56ca9" };
const result = schema.validate(data);

if (result.error) {
console.error(result.error.message); // [Error [ValidationError]: ""_id"" did not seem to be a objectId]
} else {
console.log(result); // { value: { id: new ObjectId('6752b3b125b75c18063a04b5') } }
}
```

## License

[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)

## Authors

- [@kavingovind](https://www.github.com/kavingovind)
82 changes: 82 additions & 0 deletions __tests__/objectId.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const JoiBase = require("joi");
const { JoiObjectId } = require("../dist/index.js");
const { Types } = require("mongoose");

describe("joi-mongo-objectid package", () => {
const Joi = JoiBase.extend(JoiObjectId);

it("Should validate and parse a valid MongoDB ObjectId", () => {
const schema = Joi.object({
_id: Joi.mongo().objectId().required(),
});
const data = { _id: "6a531b0516e6712d5cf56ca9" };
const result = schema.validate(data);

expect(result.error).toBeUndefined();
expect(result.value._id).toBeInstanceOf(Types.ObjectId);
expect(result.value._id.toString()).toBe("6a531b0516e6712d5cf56ca9");
});

it("Should fail validation for an invalid MongoDB ObjectId", () => {
const schema = Joi.object({
_id: Joi.mongo().objectId().required(),
});
const data = { _id: "507h1f77bcf86cd799439012" }; // _id contains 'h'
const result = schema.validate(data);

expect(result.error).toBeDefined();
expect(result.error.message).toContain("did not seem to be a objectId");
});

it("Should fail validation if ObjectId is missing", () => {
const schema = Joi.object({
_id: Joi.mongo().objectId().required(),
});
const data = {};
const result = schema.validate(data);

expect(result.error).toBeDefined();
expect(result.error.message).toContain('"_id" is required');
});

it("Should validate and parse an array of valid MongoDB ObjectIds", () => {
const schema = Joi.object({
_ids: Joi.array().items(Joi.mongo().objectId().required()).required(),
});
const data = {
_ids: ["6652b3b125b75c18063a04b5", "6a531b0516e6712d5cf56ca9"],
};
const result = schema.validate(data);

expect(result.error).toBeUndefined();
expect(result.value._ids).toHaveLength(2);
expect(result.value._ids[0]).toBeInstanceOf(Types.ObjectId);
expect(result.value._ids[1]).toBeInstanceOf(Types.ObjectId);
expect(result.value._ids[0].toString()).toBe("6652b3b125b75c18063a04b5");
expect(result.value._ids[1].toString()).toBe("6a531b0516e6712d5cf56ca9");
});

it("Should fail validation for an invalid MongoDB ObjectId in an Array", () => {
const schema = Joi.object({
_ids: Joi.array().items(Joi.mongo().objectId().required()).required(),
});
const data = {
_ids: ["507h1f77bcf86cd799439012", "6a531b0516e6712d5cf56ca9"], // _ids[0] contains 'h'
};
const result = schema.validate(data);

expect(result.error).toBeDefined();
expect(result.error.message).toContain("did not seem to be a objectId");
});

it("Should fail validation if an array is missing", () => {
const schema = Joi.object({
_ids: Joi.array().items(Joi.mongo().objectId().required()).required(),
});
const data = {};
const result = schema.validate(data);

expect(result.error).toBeDefined();
expect(result.error.message).toContain('"_ids" is required');
});
});
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
preset: "ts-jest", // Use this if you're using TypeScript
testEnvironment: "node", // Ideal for Node.js packages
verbose: true, // Provides detailed output for tests
moduleFileExtensions: ["js", "ts", "json"], // Recognize these file extensions
transform: {
"^.+\\.ts$": "ts-jest", // Transpile TypeScript
},
testMatch: ["**/__tests__/**/*.test.(ts|js)"], // Match test files
};
Loading

0 comments on commit 9c2a8b0

Please sign in to comment.