-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1af4a4
commit dc2105e
Showing
7 changed files
with
918 additions
and
331 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,78 @@ | ||
const assert = require('assert') | ||
const objectId = require('../object-id') | ||
const Joi = require('@hapi/joi') | ||
const { ObjectID } = require('mongodb') | ||
const assert = require("assert"); | ||
const objectId = require("../object-id"); | ||
const Joi = require("joi"); | ||
const { ObjectID } = require("mongodb"); | ||
|
||
const idString = '5af9f1e35b9df500148d6986' | ||
const _id = new ObjectID(idString) | ||
const idString = "5af9f1e35b9df500148d6986"; | ||
const _id = new ObjectID(idString); | ||
|
||
describe('Joi objectId validation', () => { | ||
it('updates valid objectId strings to ObjectIDs', () => { | ||
const { value } = objectId().validate(idString) | ||
describe("Joi objectId validation", () => { | ||
it("updates valid objectId strings to ObjectIDs", () => { | ||
const { value } = objectId().validate(idString); | ||
|
||
assert(value instanceof ObjectID, 'should get back an ObjectID') | ||
}) | ||
assert(value instanceof ObjectID, "should get back an ObjectID"); | ||
}); | ||
|
||
it('validates ObjectIDs', () => { | ||
const { value } = objectId().validate(_id) | ||
it("validates ObjectIDs", () => { | ||
const { value } = objectId().validate(_id); | ||
|
||
assert(_id === value, 'the _id should not be modified') | ||
assert(value instanceof ObjectID, 'should get back an ObjectID') | ||
}) | ||
assert(_id === value, "the _id should not be modified"); | ||
assert(value instanceof ObjectID, "should get back an ObjectID"); | ||
}); | ||
|
||
it('allows null to pass', () => { | ||
const { value } = objectId().validate(null) | ||
it("allows null to pass", () => { | ||
const { value } = objectId().validate(null); | ||
|
||
assert(value === null, 'the value should still be null') | ||
}) | ||
assert(value === null, "the value should still be null"); | ||
}); | ||
|
||
it('throws an error on invalid strings', () => { | ||
it("throws an error on invalid strings", () => { | ||
const userSchema = Joi.object({ | ||
userId: objectId(), | ||
name: Joi.string() | ||
}) | ||
name: Joi.string(), | ||
}); | ||
const user = { | ||
userId: 'moo', | ||
name: 'Marshall Thompson' | ||
} | ||
const { error, value } = userSchema.validate(user) | ||
assert.equal(error.message, '"userId" objectId validation failed because Argument passed in must be a single String of 12 bytes or a string of 24 hex characters') | ||
assert(value.userId === 'moo', 'the value should not have been changed') | ||
}) | ||
|
||
it('Can be used in another schema', () => { | ||
userId: "moo", | ||
name: "Marshall Thompson", | ||
}; | ||
const { error, value } = userSchema.validate(user); | ||
assert.equal( | ||
error.message, | ||
'"userId" objectId validation failed because Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' | ||
); | ||
assert(value.userId === "moo", "the value should not have been changed"); | ||
}); | ||
|
||
it("Can be used in another schema", () => { | ||
const userSchema = Joi.object({ | ||
userId: objectId(), | ||
name: Joi.string() | ||
}) | ||
name: Joi.string(), | ||
}); | ||
|
||
const user = { | ||
userId: idString, | ||
name: 'Marshall Thompson' | ||
} | ||
name: "Marshall Thompson", | ||
}; | ||
|
||
const { value } = userSchema.validate(user) | ||
const { value } = userSchema.validate(user); | ||
|
||
assert(value.userId instanceof ObjectID, 'userId should be an ObjectID') | ||
}) | ||
assert(value.userId instanceof ObjectID, "userId should be an ObjectID"); | ||
}); | ||
|
||
it('Can disallow null values', () => { | ||
it("Can disallow null values", () => { | ||
const userSchema = Joi.object({ | ||
userId: objectId().disallow(null), | ||
name: Joi.string() | ||
}) | ||
name: Joi.string(), | ||
}); | ||
|
||
const user = { | ||
userId: null, | ||
name: 'Marshall Thompson' | ||
} | ||
name: "Marshall Thompson", | ||
}; | ||
|
||
const { error, value } = userSchema.validate(user) | ||
assert.equal(error.message, '"userId" contains an invalid value') | ||
const { error, value } = userSchema.validate(user); | ||
assert.equal(error.message, '"userId" contains an invalid value'); | ||
|
||
assert(value.userId === null, 'userId is still null') | ||
}) | ||
}) | ||
assert(value.userId === null, "userId is still null"); | ||
}); | ||
}); |
Oops, something went wrong.