You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Which will allows queries like `/users?_id=507f1f77bcf86cd799439011&age=25`.
443
443
444
-
## Validating MongoDB Data
444
+
## Validate Data
445
445
446
-
### Using Resolvers
446
+
There are two ways
447
447
448
-
The simplest way to convert ObjectIds is to make a resolver.
448
+
Since MongoDB uses special binary types for stored data, Feathers uses Schemas to validate MongoDB data and resolvers to convert types. Attributes on MongoDB services often require specitying two schema formats:
449
+
450
+
- Object-type formats for data pulled from the database and passed between services on the API server. The MongoDB driver for Node converts binary data into objects, like `ObjectId` and `Date`.
451
+
- String-type formats for data passed from the client.
452
+
453
+
You can convert values to their binary types to take advantage of performance enhancements in MongoDB. The following sections show how to use Schemas and Resolvers for each MongoDB binary format.
454
+
455
+
### Shared Validators
456
+
457
+
The following example shows how to create two custom validator/type utilities:
458
+
459
+
- An ObjectId validator which handles strings or objects. Place the following code inside `src/schema/shared.ts`.
Technically, `ObjectId` in the above example could be improved since it allows any object to pass validation. A query with a bogus id fail only when it reaches the database. Since objects can't be passed from the client, it's a situation which only occurs with our server code, so it will suffice.
478
+
479
+
### ObjectIds
480
+
481
+
With the [shared validators](#shared-validators) in place, you can specify an `ObjectId` type in your TypeBox Schemas:
482
+
483
+
```ts
484
+
import { Type } from'@feathersjs/typebox'
485
+
import { ObjectId } from'../../schemas/shared'
486
+
487
+
const userSchema =Type.Object(
488
+
{
489
+
_id: ObjectId(),
490
+
orgIds: Type.Array( ObjectId() ),
491
+
},
492
+
{ $id: 'User', additionalProperties: false }
493
+
)
494
+
```
495
+
496
+
### Dates
497
+
498
+
The standard format for transmitting dates between client and server is [ISO8601](https://www.rfc-editor.org/rfc/rfc3339#section-5.6), which is a string representation of a date.
499
+
500
+
While it's possible to use `$gt` (greater than) and `$lt` (less than) queries on string values, performance will be faster for dates stored as date objects. This means you'd use the same code as the previous example, followed by a resolver or a hook to convert the values in `context.data` and `context.query` into actual Dates.
501
+
502
+
With the [shared validators](#shared-validators) in place, you can use the custom `Date` type in your TypeBox Schemas:
503
+
504
+
```ts
505
+
import { Type } from'@feathersjs/typebox'
506
+
import { ObjectId, Date } from'../../schemas/shared'
507
+
508
+
const userSchema =Type.Object(
509
+
{
510
+
_id: ObjectId(),
511
+
createdAt: Date(),
512
+
},
513
+
{ $id: 'User', additionalProperties: false }
514
+
)
464
515
```
465
516
466
-
### Using a Custom AJV Instance
517
+
##Convert Data
467
518
468
-
All [Feathers schemas](/api/schema/schema) share an implicit AJV instance by default.
519
+
It's possible to convert data by either customizing AJV or using resolvers. Converting with AJV is currently the simplest solution, but it uses extended AJV features, outside of the JSON Schema standard.
469
520
470
-
It's possible to validate MongoDB ObjectIds and dates with AJV, as well. This is more complicated than using resolvers, but can also handle the full query syntax. You can create a custom AJV instance with extra formatters attached.
521
+
### Convert With AJV
471
522
472
-
#### Custom AJV Instance
523
+
The FeathersJS CLI creates validators in the `src/schema/validators` file. It's possible to enable the AJV validators to convert certain values using AJV keywords. This example works for both [TypeBox](/api/schema/typebox) and [JSON Schema](/api/schema/schema):
473
524
474
-
Here's an example of a custom AJV instance, which could be placed in `src/schemas/ajv.ts` and referenced by all other services.
Here are a couple of errors you might run into while using validators.
618
+
We can now update the [shared validators](#shared-validators) to also convert data.
575
619
576
-
### unknown keyword: "convert"
620
+
```ts{5}
621
+
import { Type } from '@feathersjs/typebox'
577
622
578
-
You'll see an error like `"Error: strict mode: unknown keyword: "convert"` in a few scenarios:
579
-
580
-
- You fail to [Pass the Custom AJV Instance to every `schema`](#pass-the-custom-ajv-instance-to-schema). If you're using a custom AJV instance, be sure to provide it to **every** place where you call `schema()`.
581
-
- You try to use custom keywords in your schema without registering them, first.
582
-
- You make a typo in your schema. For example, it's common to forget to accidentally mis-document arrays and collapse the item `properties` up one level.
You'll see an error like `Error: unknown format "date-time" ignored in schema at path "#/properties/createdAt"` in a few scenarios.
635
+
Now when we use the shared validators (as shown in the [ObjectIds](#objectids) and [Dates](#dates) sections) AJV will also convert the data to the correct type.
587
636
588
-
- You're attempting to use a formatter not built into AJV.
589
-
- You fail to [Pass the Custom AJV Instance to every `schema`](#pass-the-custom-ajv-instance-to-schema). If you're using a custom AJV instance, be sure to provide it to **every** place where you call `schema()`.
637
+
### Convert with Resolvers
590
638
591
-
## Search
639
+
In place of [converting data with AJV](#convert-with-ajv), you can also use resolvers to convert data.
592
640
593
-
## ObjectId resolvers
641
+
####ObjectId Resolvers
594
642
595
643
MongoDB uses object ids as primary keys and for references to other documents. To a client they are represented as strings and to convert between strings and object ids, the following [property resolver](../schema/resolvers.md) helpers can be used.
596
644
597
-
### resolveObjectId
645
+
####resolveObjectId
598
646
599
647
`resolveObjectId` resolves a property as an object id. It can be used as a direct property resolver or called with the original value.
`resolveQueryObjectId` allows to query for object ids. It supports conversion from a string to an object id as well as conversion for values from the [$in, $nin and $ne query syntax](./querying.md).
Here are a couple of errors you might run into while using validators.
689
+
690
+
### unknown keyword: "convert"
691
+
692
+
You'll see an error like `"Error: strict mode: unknown keyword: "convert"` in a few scenarios:
693
+
694
+
- You fail to [Pass the Custom AJV Instance to every `schema`](#pass-the-custom-ajv-instance-to-schema). If you're using a custom AJV instance, be sure to provide it to **every** place where you call `schema()`.
695
+
- You try to use custom keywords in your schema without registering them, first.
696
+
- You make a typo in your schema. For example, it's common to forget to accidentally mis-document arrays and collapse the item `properties` up one level.
697
+
698
+
### unknown format "date-time"
699
+
700
+
You'll see an error like `Error: unknown format "date-time" ignored in schema at path "#/properties/createdAt"` in a few scenarios.
701
+
702
+
- You're attempting to use a formatter not built into AJV.
703
+
- You fail to [Pass the Custom AJV Instance to every `schema`](#pass-the-custom-ajv-instance-to-schema). If you're using a custom AJV instance, be sure to provide it to **every** place where you call `schema()`.
704
+
705
+
## Search
706
+
707
+
There are two ways to perform search queries with MongoDB:
708
+
709
+
- Perform basic Regular Expression matches using the `$regex` filter.
710
+
- Perform full-text search using the `$search` filter.
711
+
712
+
### Basic Regex Search
713
+
714
+
You can perform basic search using regular expressions with the `$regex` operator. Here's an example query.
715
+
716
+
```js
717
+
{
718
+
text: { $regex:'feathersjs', $options:'igm' },
719
+
}
720
+
```
721
+
722
+
TODO: Show how to customize the query syntax to allow the `$regex` and `$options` operators.
723
+
724
+
### Full-Text Search
725
+
726
+
See the MongoDB documentation for instructions on performing full-text search using the `$search` operator:
727
+
728
+
- Perform [full-text queries on self-hosted MongoDB](https://www.mongodb.com/docs/manual/core/link-text-indexes/).
0 commit comments