Schema
in package
schéma JSON initialisé soit par un chemin dans un fichier JSON/Yaml, soit par un array Php
Pour valider la conformité d'un fichier JSON ou Yaml à un schéma, utiliser la méthode statique Schema::autoCheck() qui prend en paramètre une instance et retourne un Status.
Une autre possibilité pour valider la conformité d'une instance définie comme valeur Php à un schéma:
- créer un objet Schema en fournissant le contenu du schema
- appeler sur cet objet la méthode check avec l'instance Php à vérifier qui retourne un Status
voir https://json-schema.org/understanding-json-schema/reference/index.html (draft-06)
3 types d'erreurs sont gérées:
- une structuration non prévue de schéma génère une exception
- une non conformité d'une instance à un schéma fait échouer la vérification
- une alerte peut être produite dans certains cas sans faire échouer la vérification
Lorsque le schéma est conforme au méta-schéma, la génération d'une exception correspond à un bug du code. Ce validateur implémente la spec http://json-schema.org/draft-06/schema# en totalité.
Table of Contents
Constants
- SCHEMAIDS = [ // liste des id acceptés pour le champ $schema 'http://json-schema.org/schema#', 'http://json-schema.org/draft-06/schema#', 'http://json-schema.org/draft-07/schema#', ]
Properties
- $def : bool|array<string|int, mixed>
- $elt : Fragment|null
- $filepath : string|null
- $status : Status
- $verbose : bool
Methods
- __construct() : mixed
- création d'un Schema
- autoCheck() : Status
- autoCheck() - valide la conformité d'une instance à son schéma défini par le champ $schema
- check() : Status
- check - vérification de la conformité d'une instance au Schema, renvoit un Status
- def() : bool|array<string|int, mixed>
- définition du contenu du schéma sous la forme d'un array Php ou un boolean
- test() : never
- checkDefinition() : void
- lance une exception si détecte une boucle dans les défs ou une référence à une déf. inexistante
Constants
SCHEMAIDS
public
mixed
SCHEMAIDS
= [
// liste des id acceptés pour le champ $schema
'http://json-schema.org/schema#',
'http://json-schema.org/draft-06/schema#',
'http://json-schema.org/draft-07/schema#',
]
Properties
$def
private
bool|array<string|int, mixed>
$def
$elt
private
Fragment|null
$elt
$filepath
private
string|null
$filepath
$status
private
Status
$status
$verbose
private
bool
$verbose
Methods
__construct()
création d'un Schema
public
__construct(bool|string|array<string|int, mixed> $def[, bool $verbose = false ][, Schema|null $parent = null ]) : mixed
Un objet Schema peut être initialisé par un fragment JSON/Yaml stocké dans un fichier et identifié par un chemin de la forme {filePath}(#{eltPath})? où:
- {filePath} identifie un fichier et peut être utilisé dans file_get_contents()
- {eltPath} est le chemin d'un élément de la forme (/{elt})+ à l'intérieur du fichier Le fichier est soit un fichier json dont l'extension doit être .json, soit un fichier yaml dont l'extension doit être .yaml ou .yml. Un Schema peut aussi être défini par un array, dans ce cas si le champ $id n'est pas défini alors les chemins de fichier utilisés dans les références vers d'autres schémas ne doivent pas être définis en relatif
Parameters
- $def : bool|string|array<string|int, mixed>
-
chemin d'un fichier contenant le JSON/Yaml, ou contenu comme array Php ou comme booléen
- $verbose : bool = false
- $parent : Schema|null = null
autoCheck()
autoCheck() - valide la conformité d'une instance à son schéma défini par le champ $schema
public
static autoCheck(mixed $instance[, array<string, string|bool> $options = [] ]) : Status
autoCheck() évalue la conformité d'une instance à son schéma défini par le champ $schema autoCheck() prend un ou 2 paramètres
- le premier paramètre est l'instance à valider soit comme valeur Php, soit le chemin du fichier la contenant
- le second paramètre d'options indique éventuellement notamment l'affichage à effectuer en fonction du résultat
de la validation ; c'est un array qui peut comprendre les champs suivants:
- showOk : chaine à afficher si ok
- showWarnings : chaine à afficher si ok avec les Warnings
- showKo : chaine à afficher si KO
- showErrors : chaine à afficher si KO avec les erreurs
- verbose : défini et vrai pour un appel verbeux, non défini ou faux pour un appel non verbeux autoCheck() renvoit un Status
Parameters
- $instance : mixed
- $options : array<string, string|bool> = []
Return values
Statuscheck()
check - vérification de la conformité d'une instance au Schema, renvoit un Status
public
check(mixed $instance[, array<string, string> $options = [] ][, string $id = '' ][, Status|null $status = null ]) : Status
Un check() prend un statut initial et le modifie pour le renvoyer à la fin
- le premier paramètre est l'instance à valider comme valeur Php
- le second paramètre indique éventuellement l'affichage à effectuer en fonction du résultat de la validation
c'est un array qui peut comprendre les champs suivants:
- showOk : chaine à afficher si ok
- showWarnings : chaine à afficher si ok avec les Warnings
- showKo : chaine à afficher si KO
- showErrors : chaine à afficher si KO avec les erreurs
- le troisième paramètre indique éventuellement un chemin utilisé dans les messages d'erreurs
- le quatrième paramètre fournit éventuellement un statut en entrée et n'est utilisé qu'en interne à la classe
Parameters
- $instance : mixed
- $options : array<string, string> = []
- $id : string = ''
- $status : Status|null = null
Return values
Statusdef()
définition du contenu du schéma sous la forme d'un array Php ou un boolean
public
def() : bool|array<string|int, mixed>
Return values
bool|array<string|int, mixed>test()
public
static test() : never
Return values
nevercheckDefinition()
lance une exception si détecte une boucle dans les défs ou une référence à une déf. inexistante
private
static checkDefinition(string $defid, array<string, mixed> $defs[, array<int, string> $defPath = [] ]) : void
Parameters
- $defid : string
- $defs : array<string, mixed>
- $defPath : array<int, string> = []