11import { analyzeDocuments } from '../../src' ;
22import Ajv2020 from 'ajv/dist/2020' ;
33import assert from 'assert' ;
4+ import { ObjectId , Int32 , Double , EJSON } from 'bson' ;
45
5- const documents = [ {
6- _id : {
7- $oid : '67863e82fb817085a6b0ebad'
8- } ,
6+ const bsonDocuments = [ {
7+ _id : new ObjectId ( '67863e82fb817085a6b0ebad' ) ,
98 title : 'My book' ,
10- year : 1983 ,
9+ year : new Int32 ( 1983 ) ,
1110 genres : [
1211 'crimi' ,
1312 'comedy' ,
@@ -16,30 +15,32 @@ const documents = [{
1615 long : 'science fiction'
1716 }
1817 ] ,
19- number : {
20- $numberDouble : 'Infinity'
21- }
18+ number : Double . fromString ( 'Infinity' )
2219} ,
2320{
24- _id : {
25- $oid : '67863eacfb817085a6b0ebae'
26- } ,
21+ _id : new ObjectId ( '67863eacfb817085a6b0ebae' ) ,
2722 title : 'Other book' ,
28- year : 1999 ,
23+ year : new Int32 ( ' 1999' ) ,
2924 author : {
3025 name : 'Peter Sonder' ,
31- rating : 1.3
26+ rating : new Double ( 1.3 )
3227 }
3328} ] ;
3429
35- describe ( 'Documents -> Generate schema -> Validate Documents against the schema' , function ( ) {
30+ describe . only ( 'Documents -> Generate schema -> Validate Documents against the schema' , function ( ) {
3631 it ( 'Standard JSON Schema with Relaxed EJSON' , async function ( ) {
3732 const ajv = new Ajv2020 ( ) ;
38- const analyzedDocuments = await analyzeDocuments ( documents ) ;
33+ // First we get the schema
34+ const analyzedDocuments = await analyzeDocuments ( bsonDocuments ) ;
3935 const schema = await analyzedDocuments . getStandardJsonSchema ( ) ;
4036 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 ) ;
4344 }
4445 } ) ;
4546} ) ;
0 commit comments