Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(schema): Add schema helper for handling Object ids #3058

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update docs
  • Loading branch information
daffl committed Feb 14, 2023
commit 4e9e510b4d519a2294a84713193233a8f0e83c88
13 changes: 10 additions & 3 deletions docs/api/databases/mongodb.md
Original file line number Diff line number Diff line change
@@ -396,15 +396,22 @@ MongoDB uses [ObjectId](https://www.mongodb.com/docs/manual/reference/method/Obj

### AJV keyword

To validate and convert strings to an object id using AJV, the `keywordObjectId` [AJV keyword](https://ajv.js.org/api.html#ajv-addkeyword-definition-string-object-ajv) helper can be used. It is set up automatically in a generated application using MongoDB. Both, `@feathersjs/typebox` and `@feathersjs/schema` export an `ObjectIdSchema` helper that creates a schema which can be both, a MongoDB ObjectId or a string that will be converted with the `objectid` keyword:
To validate and convert strings to an object id using AJV, the `keywordObjectId` [AJV keyword](https://ajv.js.org/api.html#ajv-addkeyword-definition-string-object-ajv) helper can be used. It is set up automatically in a generated application using MongoDB.

```ts
import { keywordObjectId } from '@feathersjs/mongodb'
import { ObjectIdSchema } from '@feathersjs/typebox' // or '@feathersjs/schema'

const validator = new Ajv()

validator.addKeyword(keywordObjectId)
```

### ObjectIdSchema

Both, `@feathersjs/typebox` and `@feathersjs/schema` export an `ObjectIdSchema` helper that creates a schema which can be both, a MongoDB ObjectId or a string that will be converted with the `objectid` keyword:

```ts
import { ObjectIdSchema } from '@feathersjs/typebox' // or '@feathersjs/schema'

const typeboxSchema = Type.Object({
userId: ObjectIdSchema()
@@ -420,7 +427,7 @@ const jsonSchema = {

<BlockQuote label="Important" type="warning">

Usually a converted object id property can be treated like a string but in some cases when working with it on the server you may have to call `toString()` to get the proper type.
The `ObjectIdSchema` helper will only work when the [`objectid` AJV keyword](#ajv-keyword) is registered.

</BlockQuote>