Skip to content

Commit

Permalink
yaml and json validator
Browse files Browse the repository at this point in the history
  • Loading branch information
aynsix committed Oct 11, 2024
1 parent 3009ba2 commit 2910d7f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 15 deletions.
18 changes: 9 additions & 9 deletions databox/api/config/validator/validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ App\Entity\Core\Tag:
- name
errorPath: name

App\Entity\Integration\WorkspaceIntegration:
constraints:
- App\Validator\ValidIntegrationOptionsConstraint: ~
properties:
optionsJson:
- Json:
message: You've entered an invalid Json.
optionsYaml:
- App\Validator\YamlConstraint: ~
# App\Entity\Integration\WorkspaceIntegration:
# constraints:
# - App\Validator\ValidIntegrationOptionsConstraint: ~
# properties:
# optionsJson:
# - Json:
# message: You've entered an invalid Json.
# optionsYaml:
# - App\Validator\YamlConstraint: ~

App\Entity\Integration\IntegrationData:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\DateTimeFilter;
Expand Down Expand Up @@ -69,7 +70,6 @@ public function configureFields(string $pageName): iterable
->hideOnForm();
yield DateTimeField::new('updatedAt')
->hideOnForm();
yield AssociationField::new('attributes');

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Alchemy\AdminBundle\Controller\AbstractAdminCrudController;
use Alchemy\AdminBundle\Field\IdField;
use Alchemy\AdminBundle\Field\JsonField;
use Alchemy\AdminBundle\Field\YamlField;
use App\Admin\Field\IntegrationChoiceField;
use App\Entity\Integration\WorkspaceIntegration;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
Expand All @@ -13,7 +14,6 @@
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
Expand Down Expand Up @@ -62,7 +62,7 @@ public function configureFields(string $pageName): iterable
')
->hideOnIndex();
yield $this->integrationChoiceField->create('integration');
yield TextareaField::new('configYaml', 'Config')
yield YamlField::new('configYaml', 'Config')
->onlyOnForms();
yield JsonField::new('config')
->onlyOnDetail();
Expand Down
23 changes: 23 additions & 0 deletions lib/php/admin-bundle/Field/YamlField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Alchemy\AdminBundle\Field;

use Alchemy\AdminBundle\Form\YamlType;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;

final class YamlField implements FieldInterface
{
use FieldTrait;

/**
* @param string|false|null $label
*/
public static function new(string $propertyName, $label = null): self
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
->setFormType(YamlType::class);
}
}
25 changes: 22 additions & 3 deletions lib/php/admin-bundle/Form/JsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

class JsonType extends AbstractType implements DataTransformerInterface
{
Expand All @@ -24,14 +26,31 @@ public function transform(mixed $value)

public function reverseTransform(mixed $value)
{
if ($value !== null && json_validate($value) === false) {
return ['input-error' => 'Invalid JSON: ' . json_last_error_msg()];
}

return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('attr', [
'rows' => 10,
'style' => 'font-family: "Courier New"',
$resolver->setDefaults([
'attr'=> [
'rows' => 10,
'style' => 'font-family: "Courier New"',
],
'constraints' => [
new Assert\Callback(
function (mixed $data, ExecutionContextInterface $context) {
if (isset($data['input-error'])) {
$context
->buildViolation($data['input-error'])
->addViolation();
}
}
)
]
]);
}

Expand Down
44 changes: 44 additions & 0 deletions lib/php/admin-bundle/Form/YamlType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Alchemy\AdminBundle\Form;

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

class YamlType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'attr'=> [
'rows' => 10,
'style' => 'font-family: "Courier New"',
],
'constraints' => [
new Assert\Callback(
function (mixed $data, ExecutionContextInterface $context) {
try {
Yaml::parse($data);
} catch (ParseException $e) {
$context
->buildViolation(sprintf('YAML error: %s', $e->getMessage()))
->addViolation();
}
}
)
]
]);
}

public function getParent(): ?string
{
return TextareaType::class;
}
}

0 comments on commit 2910d7f

Please sign in to comment.