Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Schema validation migration like #3225

Answered by GromNaN
jleonardolemos asked this question in Ideas
Discussion options

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?

You must be logged in to vote

We have an open ticket for this feature request: PHPORM-72

Replies: 1 comment 2 replies

Comment options

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]]);
You must be logged in to vote
2 replies
Comment options

We have an open ticket for this feature request: PHPORM-72

Answer selected by jleonardolemos
Comment options

OK. I will use the createCollection approach for now. Thanks by the response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /