Skip to content

Commit

Permalink
add bundle validation
Browse files Browse the repository at this point in the history
  • Loading branch information
matronator committed Sep 26, 2022
1 parent f1371ac commit 515aa33
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
parsem.sketch
.DS_Store
27 changes: 26 additions & 1 deletion src/Parsem/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,32 @@ public static function isValid(string $filename, ?string $contents = null): bool
}

$validator = new Validator();
$schema = file_get_contents('https://files.matronator.com/public/mtrgen/latest/mtrgen-template-schema.json');
$schema = file_get_contents('https://www.mtrgen.com/storage/schemas/template/latest/mtrgen-template-schema.json');
$result = $validator->validate($parsed, $schema);

return $result->isValid();
}

/**
* Validate the file against a template bundle schema and return the result
* @return boolean True if the file is a valid bundle, false otherwise.
* @param string $filename
* @param string|null $contents [optional] You can also provide the file's content as a string, but you still have to provide a `$filename` to know which format to parse (YAML, JSON or NEON).
*/
public static function isValidBundle(string $filename, ?string $contents = null): bool
{
if (!preg_match('/^.+?(\.bundle)\..+?$/', $filename, $matches)) {
return false;
}

try {
$parsed = self::decodeByExtension($filename, $contents);
} catch (Exception $e) {
return false;
}

$validator = new Validator();
$schema = file_get_contents('https://www.mtrgen.com/storage/schemas/bundle/latest/mtrgen-bundle-schema.json');
$result = $validator->validate($parsed, $schema);

return $result->isValid();
Expand Down

0 comments on commit 515aa33

Please sign in to comment.