1
1
import { analyzeDocuments } from '../../src' ;
2
2
import Ajv2020 from 'ajv/dist/2020' ;
3
3
import assert from 'assert' ;
4
+ import { ObjectId , Int32 , Double , EJSON } from 'bson' ;
4
5
5
- const documents = [ {
6
- _id : {
7
- $oid : '67863e82fb817085a6b0ebad'
8
- } ,
6
+ const bsonDocuments = [ {
7
+ _id : new ObjectId ( '67863e82fb817085a6b0ebad' ) ,
9
8
title : 'My book' ,
10
- year : 1983 ,
9
+ year : new Int32 ( 1983 ) ,
11
10
genres : [
12
11
'crimi' ,
13
12
'comedy' ,
@@ -16,30 +15,32 @@ const documents = [{
16
15
long : 'science fiction'
17
16
}
18
17
] ,
19
- number : {
20
- $numberDouble : 'Infinity'
21
- }
18
+ number : Double . fromString ( 'Infinity' )
22
19
} ,
23
20
{
24
- _id : {
25
- $oid : '67863eacfb817085a6b0ebae'
26
- } ,
21
+ _id : new ObjectId ( '67863eacfb817085a6b0ebae' ) ,
27
22
title : 'Other book' ,
28
- year : 1999 ,
23
+ year : new Int32 ( ' 1999' ) ,
29
24
author : {
30
25
name : 'Peter Sonder' ,
31
- rating : 1.3
26
+ rating : new Double ( 1.3 )
32
27
}
33
28
} ] ;
34
29
35
- describe ( 'Documents -> Generate schema -> Validate Documents against the schema' , function ( ) {
30
+ describe . only ( 'Documents -> Generate schema -> Validate Documents against the schema' , function ( ) {
36
31
it ( 'Standard JSON Schema with Relaxed EJSON' , async function ( ) {
37
32
const ajv = new Ajv2020 ( ) ;
38
- const analyzedDocuments = await analyzeDocuments ( documents ) ;
33
+ // First we get the schema
34
+ const analyzedDocuments = await analyzeDocuments ( bsonDocuments ) ;
39
35
const schema = await analyzedDocuments . getStandardJsonSchema ( ) ;
40
36
const validate = ajv . compile ( schema ) ;
41
- for ( const doc of documents ) {
42
- assert . strictEqual ( validate ( doc ) , true ) ;
37
+ for ( const doc of bsonDocuments ) {
38
+ // Then we get EJSON documents
39
+ const relaxedEJSONDoc = EJSON . serialize ( doc , { relaxed : true } ) ;
40
+ // Which we validate against the schema
41
+ const valid = validate ( relaxedEJSONDoc ) ;
42
+ if ( validate . errors ) console . error ( 'Validation failed' , validate . errors ) ;
43
+ assert . strictEqual ( valid , true ) ;
43
44
}
44
45
} ) ;
45
46
} ) ;
0 commit comments