Define schema validation in Laravel migrations

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Fixed
    • Priority: Unknown
    • laravel-5.5.0
    • Affects Version/s: None
    • Component/s: Laravel
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      That the `$jsonSchema` operator (for adding schema validation) is now supported through Laravel's `Schema::create` (when creating a new collection) and `Schema::table` (when updating a new collection) methods. In the callback to those methods, users can call `$table->jsonSchema()` passing the schema array, and optionally the validation level and validation action.

      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      Yes

      3. Which versions of the driver/connector does this apply to?
      5.5

      Show
      1. What would you like to communicate to the user about this feature? That the `$jsonSchema` operator (for adding schema validation) is now supported through Laravel's `Schema::create` (when creating a new collection) and `Schema::table` (when updating a new collection) methods. In the callback to those methods, users can call `$table->jsonSchema()` passing the schema array, and optionally the validation level and validation action. 2. Would you like the user to see examples of the syntax and/or executable code and its output? Yes 3. Which versions of the driver/connector does this apply to? 5.5
    • 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:
              Resolved: