Laravel-mongodb: Document Validation Schema

Created on 27 May 2016  路  8Comments  路  Source: jenssegers/laravel-mongodb

as from version 3.2 MongoDB supports natively document validation
https://docs.mongodb.com/manual/core/document-validation/

is this currently supported or is there a plan to add it?

Most helpful comment

Init your own builder within the up() method of your migration file, and you're good to go:

use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Schema\Builder;

public function up()
{
    $connection = new Connection(
        Config::get('database.connections.mongodb_test')
    );
    $builder = new Builder($connection);

    $builder->create('users', function(Blueprint $collection){
        $collection->unique('username');
        $collection->unique('email');
    }, [
        'validator' => [
            '$jsonSchema' => [
                'bsonType' => 'object',
                'required' => [
                    'username',
                    'email',
                    'password',
                ],
                'additionalProperties' => false,
                'properties' => [
                    '_id' => [
                        'bsonType' => 'objectId'
                    ],
                    'username' => [
                        'bsonType' => 'string',
                        'minLength' => 8,
                        'maxLength' => 8
                    ],
                    'email' => [
                        'bsonType' => 'string',
                        'maxLength' => 255
                    ],
                    'password' => [
                        'bsonType' => 'string',
                        'maxLength' => 255
                    ]
                ]
            ]
        ],
        'validationLevel' => 'strict',
        'validationAction' => 'error'
    ]);
}

All 8 comments

+1

any update on this?

Also interested in knowing if this is implemented

+1

Why was this closed? I am looking for this feature as well but I can't see it in the docs.

Sorry if it is a dumb question, I am new to this package.

I think all you need is https://github.com/suren1986/laravel-mongo-model-schema read also
https://packagist.org/packages/suren/laravel-mongo-model-schema

Thank you very much @Modex987, I'll check it out

Init your own builder within the up() method of your migration file, and you're good to go:

use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Schema\Builder;

public function up()
{
    $connection = new Connection(
        Config::get('database.connections.mongodb_test')
    );
    $builder = new Builder($connection);

    $builder->create('users', function(Blueprint $collection){
        $collection->unique('username');
        $collection->unique('email');
    }, [
        'validator' => [
            '$jsonSchema' => [
                'bsonType' => 'object',
                'required' => [
                    'username',
                    'email',
                    'password',
                ],
                'additionalProperties' => false,
                'properties' => [
                    '_id' => [
                        'bsonType' => 'objectId'
                    ],
                    'username' => [
                        'bsonType' => 'string',
                        'minLength' => 8,
                        'maxLength' => 8
                    ],
                    'email' => [
                        'bsonType' => 'string',
                        'maxLength' => 255
                    ],
                    'password' => [
                        'bsonType' => 'string',
                        'maxLength' => 255
                    ]
                ]
            ]
        ],
        'validationLevel' => 'strict',
        'validationAction' => 'error'
    ]);
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

lgt picture lgt  路  3Comments

Idnan picture Idnan  路  3Comments

viacheslavpleshkov picture viacheslavpleshkov  路  3Comments

sanjay1688 picture sanjay1688  路  3Comments

naveedyasin picture naveedyasin  路  3Comments