Schema validation migration like #3225
-
|
MongoDB has built in schema validations. @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.
Replies: 2 comments 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.
-
|
I was working on some proof of concept here: dercoder#1
Schema::create('users', function (Blueprint $collection) {
$collection->string('name');
$collection->object('address'); // Optional: explicitly define the object
$collection->string('address.street');
$collection->string('address.city')->nullable();
$collection->string('address.zip', 10);
$collection->array('phoneNumbers', 'string');
});Result: MongoDB will enforce that address is an object, address.street is a required string, and address.zip has a maximum length of 10.
Schema::create('posts', function (Blueprint $collection) {
$collection->string('title');
$collection->array('tags', 'object'); // Define 'tags' as an array of objects
$collection->string('tags.*.name');
$collection->string('tags.*.slug')->nullable();
});Result: Every object inside the tags array must have a name string. If an item does not match this schema, MongoDB will reject the write.
Schema::create('orders', function (Blueprint $collection) {
$collection->array('items', 'object');
$collection->string('items.*.product.name');
$collection->integer('items.*.product.id');
$collection->decimal('items.*.price');
});
Schema::table('users', function (Blueprint $collection) {
// 1. Removes 'phoneNumbers' from documents and from the required properties in the schema
$collection->dropColumn('phoneNumbers');
// 2. Removes 'zip' from the nested 'address' object in both schema and data
$collection->dropColumn('address.zip');
// 3. Removes 'metadata' from all items within the 'items' array
$collection->dropColumn('items.*.metadata');
}); |
Beta Was this translation helpful? Give feedback.
We have an open ticket for this feature request: PHPORM-72