Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json Schema validation #9

Open
fesor opened this issue Jul 18, 2015 · 0 comments
Open

Json Schema validation #9

fesor opened this issue Jul 18, 2015 · 0 comments
Assignees
Milestone

Comments

@fesor
Copy link
Owner

fesor commented Jul 18, 2015

Json Schema is very powerful tool to verify JSON responses. But it not so user friendly and very verbose. Custom schema definitions and refs makes life a little bit easier but not so much.

I like the way type declarations made in RAML 1.0. It has a lot of sugar, it allows users to define user types, extending primitives. So we need to implement something like this:

// register custom type
JsonMatcher\Types::register('email', [
    'pattern' => '^.+\@\S+\.\S+'
]);

// or register multiple types:
JsonMatcher\Types::register([
    'integer' => [
         'type' => 'number',
         'multipleOf' => 1.0
    ],
    'positiveInteger' => [
         'type' => 'integer',
         'minimum' => 0,
         'exclusiveMinimum' => true
    ],
    'UserRegisterRequest' => [
         'properties' => [
              'email' => 'email',
              'password' => [
                   'minLength' => 4
              ],
              'first_name?' => 'string',
              'last_name?' => 'string'
         ]
    ],
]);

And then we can just use it:

$json->matches('UserRegisterRequest');
// or we can use inline type declarations
$json->matches([
    'type' => 'UserRegisterRequest',
    'properties' => [
        'foo' => 'string'
    ]
]);

Type expressions

Type expressions are fully stolen from RAML:

Type expressions provide a powerful way of referring to, and even defining, types. Type expressions can be used wherever a type is expected. The simplest type expression is just the name of a type. Using type expressions, you can devise type unions, arrays, maps, and other things.

Expression Description
Person The simplest type expression: A single type
Person[] An array of Person objects
string[] An array of string scalars
string[][] A bi-dimensional array of string scalars
`string Person`
`(string Person)[]`
@fesor fesor added this to the v1.0.0 milestone Jul 18, 2015
@fesor fesor self-assigned this Jul 28, 2015
@fesor fesor modified the milestones: v0.3, v1.0.0 May 17, 2016
@fesor fesor mentioned this issue May 17, 2016
4 tasks
@fesor fesor modified the milestones: v1.0.0, v0.3 May 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant