-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Schema validation migration like #3225
-
MongoDB has built in schema validations.
Today is possible to use these schema validations at the Driver level but would be nice if the Laravel Mongodb package has some "Migration Like" approach to deal with this.
@alcaeus Is it something the the maintainers would like to have?? Are you open to a PR at this subject?
Beta Was this translation helpful? Give feedback.
All reactions
We have an open ticket for this feature request: PHPORM-72
Replies: 1 comment 2 replies
-
The schema validation in MongoDB is optional. I understand that you'd like to use the same schema methods as for a SQL database to define fields and impose their name and type.
Currently this is effectively ignored.
What you want:
Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name')->comment('Name is required and must be a string'); $table->string('email')->regex('^.+@.+\..+$')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });
To create the collection like this:
$jsonSchema = [ 'bsonType' => 'object', 'required' => ['_id', 'name', 'email', 'password'], 'properties' => [ '_id' => [ 'bsonType' => 'objecId', ], 'name' => [ 'bsonType' => 'string', 'description' => 'Name is required and must be a string', ], 'email' => [ 'bsonType' => 'string', 'pattern' => '^.+@.+\..+$', ], 'email_verified_at' => [ 'bsonType' => ['date', 'null'], ], 'password' => [ 'bsonType' => 'string', ], 'remember_token' => [ 'bsonType' => ['string', 'null'], ], 'created_at' => [ 'bsonType' => 'date', ], 'updated_at' => [ 'bsonType' => 'date', ], ], ]; // Create collection with validation $database->createCollection('users', ['validator' => ['$jsonSchema' => $jsonSchema]]);
Beta Was this translation helpful? Give feedback.
All reactions
-
We have an open ticket for this feature request: PHPORM-72
Beta Was this translation helpful? Give feedback.
All reactions
-
OK. I will use the createCollection approach for now. Thanks by the response.
Beta Was this translation helpful? Give feedback.