Define schema validation in Laravel migrations

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: Laravel
    • None
    • None
    • None
    • None
    • None
    • None

      The schema constraint can be added during collection creation or updated after: https://www.mongodb.com/docs/manual/core/schema-validation/update-schema-validation/#modify-the-validation-schema. 
      Laravel Eloquent migrations has a schema feature that is used to create SQL tables, and Collection indexes in the context of MongoDB. We don't want to generate the JSON schema from the Eloquent schema builder methods (string, increment...) https://laravel.com/docs/12.x/migrations#migration-structure 

      Add a new method Blueprint::jsonSchema() that updates the JSON schema of the collection.
      Usage: 

          Schema::create('flights', function (Blueprint $collection) {
             $collection->jsonSchema([
               'bsonType' => 'object',
               'required' => [ 'username', 'password' ],
               'properties' => [
                  'username' => [
                     'bsonType' => 'string',
                     'description' => 'must be a string and is required',
                  ],
                  'password' => [
                     'bsonType' => 'string',
                     'minLength' => 8,
                     'description' => 'must be a string at least 8 characters long, and is required',
                  ]
               ]
            ]);
          });

      This feature should be covered by the documentation.

            Assignee:
            Pauline Vos
            Reporter:
            Jérôme Tamarelle
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: