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: 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.
We have an open ticket for this feature request: PHPORM-72