File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed
Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -117,6 +117,20 @@ public function hasIndex($indexOrColumns = null)
117117 return false ;
118118 }
119119
120+ public function jsonSchema (
121+ array $ schema = [],
122+ ?string $ validationLevel = null ,
123+ ?string $ validationAction = null ,
124+ ): void {
125+ $ this ->connection ->getDatabase ()->modifyCollection ($ this ->collection ->getCollectionName (), [
126+ 'validator ' => [
127+ '$jsonSchema ' => $ schema ,
128+ ],
129+ 'validationLevel ' => $ validationLevel ,
130+ 'validationAction ' => $ validationAction ,
131+ ]);
132+ }
133+
120134 /**
121135 * @param string|array $indexOrColumns
122136 *
Original file line number Diff line number Diff line change @@ -63,6 +63,39 @@ public function testCreateWithOptions(): void
6363 $ this ->assertEquals (1024 , $ collection ['options ' ]['size ' ]);
6464 }
6565
66+ public function testCreateWithSchemaValidator (): void
67+ {
68+ $ schema = [
69+ 'bsonType ' => 'object ' ,
70+ 'required ' => [ 'username ' ],
71+ 'properties ' => [
72+ 'username ' => [
73+ 'bsonType ' => 'string ' ,
74+ 'description ' => 'must be a string and is required ' ,
75+ ],
76+ ],
77+ ];
78+
79+ Schema::create (self ::COLL_2 , function (Blueprint $ collection ) use ($ schema ) {
80+ $ collection ->string ('username ' );
81+ $ collection ->jsonSchema (schema: $ schema , validationAction: 'warn ' );
82+ });
83+
84+ $ this ->assertTrue (Schema::hasCollection (self ::COLL_2 ));
85+ $ this ->assertTrue (Schema::hasTable (self ::COLL_2 ));
86+
87+ $ collection = Schema::getCollection (self ::COLL_2 );
88+ $ this ->assertEquals (
89+ ['$jsonSchema ' => $ schema ],
90+ $ collection ['options ' ]['validator ' ],
91+ );
92+
93+ $ this ->assertEquals (
94+ 'warn ' ,
95+ $ collection ['options ' ]['validationAction ' ],
96+ );
97+ }
98+
6699 public function testDrop (): void
67100 {
68101 Schema::create (self ::COLL_1 );
You can’t perform that action at this time.
0 commit comments