<commit_before>---
layout: default
permalink: simple-example
title: Simple example
---

# Simple Example

To demonstrate how to use this package, let's validate some JSON against the JSON that defines the JSON schema itself.

First we create a dereferencer, and dereference the schema.  This resolves any JSON like `{"$ref" "#"}` into reference objects, which are required for the validator to resolve `$ref` keywords properly.  Make sure you installed [league/json-reference](//json-refererence.thephpleague.com) if you are using references.

Next we create the validator.  The first argument is the data we are validating.  The second argument is the dereferenced schema.

Once the validator is created you can call `$validator->passes()` or `$validator->fails()` to validate the schema.  If the validator fails, you can view the errors by calling `$validator->errors()`.

```php
<?php

$dereferencer  = League\JsonReference\Dereferencer::draft4();
$schema        = $dereferencer->dereference('http://json-schema.org/draft-04/schema#');
$data          = json_decode('{ "id": "json-guard.dev/schema#" }');

$validator     = new League\JsonGuard\Validator($data, $schema);

if ($validator->fails()) {
    $errors = $validator->errors();
}
```
<commit_msg>Update simple-example.md<commit_after>---
layout: default
permalink: simple-example
title: Simple example
---

# Simple Example

To demonstrate how to use this package, let's validate some JSON against the JSON that defines the JSON schema itself.

First we create a dereferencer, and dereference the schema.  This resolves any JSON like `{"$ref" "#"}` into reference objects, which are required for the validator to resolve `$ref` keywords properly.  Make sure you installed [league/json-reference](//json-reference.thephpleague.com) if you are using references.

Next we create the validator.  The first argument is the data we are validating.  The second argument is the dereferenced schema.

Once the validator is created you can call `$validator->passes()` or `$validator->fails()` to validate the schema.  If the validator fails, you can view the errors by calling `$validator->errors()`.

```php
<?php

$dereferencer  = League\JsonReference\Dereferencer::draft4();
$schema        = $dereferencer->dereference('http://json-schema.org/draft-04/schema#');
$data          = json_decode('{ "id": "json-guard.dev/schema#" }');

$validator     = new League\JsonGuard\Validator($data, $schema);

if ($validator->fails()) {
    $errors = $validator->errors();
}
```

{'fraction_non_alphanumeric': 0.12306470821754664, 'fraction_numerical': 0.0023818975784041284, 'mean_word_length': 4.454545454545454, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6400202', 'n_tokens_mistral': 724, 'n_tokens_neox': 713, 'n_words': 271}