AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PlanValidator.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\StorePlan;
  3. use Illuminate\Support\Facades\Validator;
  4. use Illuminate\Validation\Rule;
  5. use Illuminate\Validation\ValidationException;
  6. /**
  7. * Checks that a store plan follows PlanSchema, and strips anything the schema does not know.
  8. *
  9. * Every plan passes through here before it is saved, no matter who wrote it.
  10. * This is what stops a weak or misbehaving AI model from breaking a store.
  11. */
  12. final class PlanValidator
  13. {
  14. /**
  15. * Return the cleaned plan, or throw if it is not valid.
  16. *
  17. * @param array<mixed> $plan
  18. * @return array<string, mixed>
  19. *
  20. * @throws ValidationException
  21. */
  22. public function validate(array $plan): array
  23. {
  24. $rules = [
  25. 'version' => ['required', 'integer', Rule::in([PlanSchema::VERSION])],
  26. 'theme' => ['required', 'array'],
  27. 'theme.colors' => ['required', 'array'],
  28. 'theme.fonts' => ['required', 'array'],
  29. 'theme.fonts.heading' => ['required', Rule::in(array_keys(PlanSchema::FONTS))],
  30. 'theme.fonts.body' => ['required', Rule::in(array_keys(PlanSchema::FONTS))],
  31. 'theme.radius' => ['required', Rule::in(PlanSchema::RADII)],
  32. 'sections' => ['required', 'array', 'min:1', 'max:'.PlanSchema::MAX_SECTIONS],
  33. 'sections.*' => ['required', 'array'],
  34. 'sections.*.id' => ['required', 'string', 'distinct', 'regex:'.PlanSchema::SECTION_ID_PATTERN],
  35. 'sections.*.type' => ['required', Rule::in(array_keys(PlanSchema::SECTIONS))],
  36. 'sections.*.content' => ['required', 'array'],
  37. ];
  38. foreach (PlanSchema::COLORS as $color) {
  39. $rules['theme.colors.'.$color] = ['required', 'string', 'regex:'.PlanSchema::COLOR_PATTERN];
  40. }
  41. // Each section gets rules that depend on its own type.
  42. foreach ($this->sections($plan) as $index => $section) {
  43. $type = is_array($section) ? ($section['type'] ?? null) : null;
  44. if (! is_string($type) || ! isset(PlanSchema::SECTIONS[$type])) {
  45. continue;
  46. }
  47. $rules["sections.$index.variant"] = ['required', Rule::in(PlanSchema::SECTIONS[$type])];
  48. foreach (PlanSchema::contentRules($type) as $field => $fieldRules) {
  49. $rules["sections.$index.content.$field"] = array_map(
  50. fn (string $rule) => $this->qualify($rule, "sections.$index.content."),
  51. $fieldRules,
  52. );
  53. }
  54. }
  55. /** @var array<string, mixed> $clean */
  56. $clean = Validator::make($plan, $rules)->validate();
  57. return $clean;
  58. }
  59. /**
  60. * Whether the plan is valid, without throwing.
  61. *
  62. * @param array<mixed> $plan
  63. */
  64. public function passes(array $plan): bool
  65. {
  66. try {
  67. $this->validate($plan);
  68. return true;
  69. } catch (ValidationException) {
  70. return false;
  71. }
  72. }
  73. /**
  74. * @param array<mixed> $plan
  75. * @return array<mixed>
  76. */
  77. private function sections(array $plan): array
  78. {
  79. return is_array($plan['sections'] ?? null) ? $plan['sections'] : [];
  80. }
  81. /**
  82. * Rules such as "required_with:cta" name another field. Point them at the full path.
  83. */
  84. private function qualify(string $rule, string $prefix): string
  85. {
  86. if (str_starts_with($rule, 'required_with:')) {
  87. return 'required_with:'.$prefix.substr($rule, strlen('required_with:'));
  88. }
  89. return $rule;
  90. }
  91. }