Skip to content

Custom coercion to types. #524

Closed
Closed
@adjenks

Description

@adjenks

I believe you should be able to enable more elaborate coercion of data types.
For example, coercing 'on' to true, or 'off' to false.
This could be implemented via a custom callback.
Just swap out functions like TypeConstrain->toBoolean() with optional custom callbacks.
For example:

$v = new \JsonSchema\Validator();
$v->setBooleanCoercion(function ($value)
    {
        if ($value === 'true' || $value === 'on') {
            return true;
        }

        if ($value === 'false' || $value === 'off') {
            return false;
        }

        return $value;
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions