AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PlanValidator.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. 'pages' => ['required', 'array', 'min:1', 'max:'.PlanSchema::MAX_PAGES],
  33. 'pages.*' => ['required', 'array'],
  34. 'pages.*.id' => ['required', 'string', 'distinct', 'regex:'.PlanSchema::SECTION_ID_PATTERN, Rule::notIn(PlanSchema::RESERVED_PAGE_IDS)],
  35. 'pages.*.title' => ['required', 'string', 'max:60'],
  36. 'pages.*.sections' => ['required', 'array', 'min:1', 'max:'.PlanSchema::MAX_SECTIONS],
  37. 'pages.*.sections.*' => ['required', 'array'],
  38. 'pages.*.sections.*.id' => ['required', 'string', 'regex:'.PlanSchema::SECTION_ID_PATTERN],
  39. 'pages.*.sections.*.type' => ['required', Rule::in(array_keys(PlanSchema::pageSections()))],
  40. 'pages.*.sections.*.content' => ['required', 'array'],
  41. // The first page is always the home page.
  42. 'pages.0.id' => ['required', Rule::in([PlanSchema::HOME_PAGE])],
  43. ];
  44. foreach (PlanSchema::COLORS as $color) {
  45. $rules['theme.colors.'.$color] = ['required', 'string', 'regex:'.PlanSchema::COLOR_PATTERN];
  46. }
  47. // The header and footer are shared by every page.
  48. foreach (['header', 'footer'] as $part) {
  49. $rules[$part] = ['required', 'array'];
  50. $rules["$part.content"] = ['required', 'array'];
  51. $rules += $this->sectionRules($part, $part);
  52. }
  53. // Each section gets rules that depend on its own type.
  54. foreach ($this->list($plan, 'pages') as $pageIndex => $page) {
  55. foreach ($this->list($page, 'sections') as $sectionIndex => $section) {
  56. $type = is_array($section) ? ($section['type'] ?? null) : null;
  57. if (is_string($type) && isset(PlanSchema::pageSections()[$type])) {
  58. $rules += $this->sectionRules($type, "pages.$pageIndex.sections.$sectionIndex");
  59. }
  60. }
  61. }
  62. /** @var array<string, mixed> $clean */
  63. $clean = Validator::make($plan, $rules)->validate();
  64. $this->assertSectionIdsAreUnique($clean);
  65. return $clean;
  66. }
  67. /**
  68. * Whether the plan is valid, without throwing.
  69. *
  70. * @param array<mixed> $plan
  71. */
  72. public function passes(array $plan): bool
  73. {
  74. try {
  75. $this->validate($plan);
  76. return true;
  77. } catch (ValidationException) {
  78. return false;
  79. }
  80. }
  81. /**
  82. * The variant and content rules for one section, placed at the given path in the plan.
  83. *
  84. * @return array<string, list<mixed>>
  85. */
  86. private function sectionRules(string $type, string $path): array
  87. {
  88. $rules = ["$path.variant" => ['required', Rule::in(PlanSchema::SECTIONS[$type])]];
  89. foreach (PlanSchema::contentRules($type) as $field => $fieldRules) {
  90. $rules["$path.content.$field"] = array_map(
  91. fn (string $rule) => $this->qualify($rule, "$path.content."),
  92. $fieldRules,
  93. );
  94. }
  95. return $rules;
  96. }
  97. /**
  98. * A section id must be unique across the whole plan, so links and chat edits can name one section.
  99. *
  100. * @param array<string, mixed> $plan
  101. *
  102. * @throws ValidationException
  103. */
  104. private function assertSectionIdsAreUnique(array $plan): void
  105. {
  106. $seen = [];
  107. foreach ($plan['pages'] as $pageIndex => $page) {
  108. foreach ($page['sections'] as $sectionIndex => $section) {
  109. if (isset($seen[$section['id']])) {
  110. throw ValidationException::withMessages([
  111. "pages.$pageIndex.sections.$sectionIndex.id" => "The section id [{$section['id']}] is used more than once. Every section id must be unique in the whole plan.",
  112. ]);
  113. }
  114. $seen[$section['id']] = true;
  115. }
  116. }
  117. }
  118. /**
  119. * @return array<mixed>
  120. */
  121. private function list(mixed $value, string $key): array
  122. {
  123. return is_array($value) && is_array($value[$key] ?? null) ? $value[$key] : [];
  124. }
  125. /**
  126. * Rules such as "required_with:cta" name another field. Point them at the full path.
  127. */
  128. private function qualify(string $rule, string $prefix): string
  129. {
  130. if (str_starts_with($rule, 'required_with:')) {
  131. return 'required_with:'.$prefix.substr($rule, strlen('required_with:'));
  132. }
  133. return $rule;
  134. }
  135. }