vanilla/garden-schema

Create Schema::add() is weird

DaazKu opened this issue · 1 comments

Currently if we want to create a subset of a schema, given the following schema:

    /**
     * Get a schema instance comprised of all available comment fields.
     *
     * @return Schema Returns a schema object.
     */
    protected function fullSchema() {
        return Schema::parse([
            'commentID:i' => 'The ID of the comment.',
            'discussionID:i' => 'The ID of the discussion.',
            'body:s' => 'The body of the comment.',
            'format:s' => 'The output format of the comment.',
            'dateInserted:dt' => 'When the comment was created.',
            'insertUserID:i' => 'The user that created the comment.',
            'insertUser?' => $this->getUserFragmentSchema(),
            'url:s?' => 'The full URL to the comment.'
        ]);
    }

We have to do:

$this->schema(['commentID', 'discussionID', 'body', 'format'], 'out')->add($this->fullSchema());

This is counter intuitive. Expected result from reading the code would be the fullSchema.
I propose that we create the subSet() function instead.

$this->fullSchema()->subset(['commentID', 'discussionID', 'body', 'format']); 

This is clearer than its counterpart.

Then we should have a fill() utility method that does what add($data, false) does and add() should be refactored to drop $addProperries and have it always add missing properties.

TL;DR;
Create subset() and fill() and make add() only add missing properties.

I've just realized that Schema is really just a wrapper around an array.
The correct way to get a subset of a schema properties would be:

array_intersect_key($schema['properties'], array_flip(['discussionID', 'name', 'body', 'format', 'categoryID']))