Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
.DS_Store
/vendor/
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

`jsv4-php` is a data validator, using version 4 JSON Schemas.

Just include `jsv4.php` from your code, and use the static methods on the `Jsv4` class it defines.
Use composer autoload to load Jsv4 classes, and use the static methods on the `Jsv4` class it defines.

Usage:

### `Jsv4::validate($data, $schema)`
### `Jsv4\Jsv4::validate($data, $schema)`

This usage returns an object of the following shape.
```json
Expand All @@ -33,17 +33,17 @@ The values in the `errors` array are similar to those for [tv4](https://github.c

The `code` property corresponds to a constant corresponding to the nature of the validation error, e.g. `JSV4_INVALID_TYPE`. The names of these constants (and their values) match up exactly with the [constants from tv4](https://github.com/geraintluff/tv4/blob/master/source/api.js).

### `Jsv4::isValid($data, $schema)`
### `Jsv4\Jsv4::isValid($data, $schema)`

If you just want to know the validation status, and don't care what the errors actually are, then this is a more concise way of getting it.

It returns a boolean indicating whether the data correctly followed the schema.

### `Jsv4::coerce($data, $schema)`
### `Jsv4\Jsv4::coerce($data, $schema)`

Sometimes, the data is not quite the correct shape - but it could be *made* the correct shape by simple modifications.

If you call `Jsv4::coerce($data, $schema)`, then it will attempt to change the data.
If you call `Jsv4\Jsv4::coerce($data, $schema)`, then it will attempt to change the data.

If it is successful, then a modified version of the data can be found in `$result->value`.

Expand All @@ -53,19 +53,19 @@ It's not psychic - in fact, it's quite limited. What it currently does is:

Perhaps you are using data from `$_GET`, so everything's a string, but the schema says certain values should be integers or booleans.

`Jsv4::coerce()` will attempt to convert strings to numbers/booleans *only where the schema says*, leaving other numerically-value strings as strings.
`Jsv4\Jsv4::coerce()` will attempt to convert strings to numbers/booleans *only where the schema says*, leaving other numerically-value strings as strings.

### Missing properties

Perhaps the API needs a complete object (described using `"required"` in the schema), but only a partial one was supplied.

`Jsv4::coerce()` will attempt to insert appropriate values for the missing properties, using a default (if it is defined in a nearby `"properties"` entry) or by creating a value if it knows the type.
`Jsv4\Jsv4::coerce()` will attempt to insert appropriate values for the missing properties, using a default (if it is defined in a nearby `"properties"` entry) or by creating a value if it knows the type.

## The `SchemaStore` class

This class represents a collection of schemas. You include it from `schema-store.php`, and use it like this:
```php
$store = new SchemaStore();
$store = new Jsv4\SchemaStore();
$store->add($url, $schema);
$retrieved = $store->get($url);
```
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

},
"autoload": {
"classmap": [
"jsv4.php",
"schema-store.php"
]
"psr-4": {
"Jsv4\\": "src/Jsv4"
}
}
}
}
Loading