$plan * @return array * * @throws ValidationException */ public function validate(array $plan): array { $rules = [ 'version' => ['required', 'integer', Rule::in([PlanSchema::VERSION])], 'theme' => ['required', 'array'], 'theme.colors' => ['required', 'array'], 'theme.fonts' => ['required', 'array'], 'theme.fonts.heading' => ['required', Rule::in(array_keys(PlanSchema::FONTS))], 'theme.fonts.body' => ['required', Rule::in(array_keys(PlanSchema::FONTS))], 'theme.radius' => ['required', Rule::in(PlanSchema::RADII)], // Optional: an old plan without it uses PlanSchema::STYLE_DEFAULTS. 'theme.style' => ['nullable', 'array'], 'pages' => ['required', 'array', 'min:1', 'max:'.PlanSchema::MAX_PAGES], 'pages.*' => ['required', 'array'], 'pages.*.id' => ['required', 'string', 'distinct', 'regex:'.PlanSchema::SECTION_ID_PATTERN, Rule::notIn(PlanSchema::RESERVED_PAGE_IDS)], 'pages.*.title' => ['required', 'string', 'max:60'], 'pages.*.sections' => ['required', 'array', 'min:1', 'max:'.PlanSchema::MAX_SECTIONS], 'pages.*.sections.*' => ['required', 'array'], 'pages.*.sections.*.id' => ['required', 'string', 'regex:'.PlanSchema::SECTION_ID_PATTERN], 'pages.*.sections.*.type' => ['required', Rule::in(array_keys(PlanSchema::pageSections()))], 'pages.*.sections.*.content' => ['required', 'array'], // The first page is always the home page. 'pages.0.id' => ['required', Rule::in([PlanSchema::HOME_PAGE])], ]; // A heading-only font (all capitals or handwriting) would make paragraphs hard to read. $rules['theme.fonts.body'][] = Rule::notIn(PlanSchema::HEADING_ONLY_FONTS); foreach (PlanSchema::STYLE_OPTIONS as $option => $values) { $rules['theme.style.'.$option] = ['nullable', Rule::in($values)]; } foreach (PlanSchema::COLORS as $color) { $rules['theme.colors.'.$color] = ['required', 'string', 'regex:'.PlanSchema::COLOR_PATTERN]; } // The header and footer are shared by every page. foreach (['header', 'footer'] as $part) { $rules[$part] = ['required', 'array']; $rules["$part.content"] = ['required', 'array']; $rules += $this->sectionRules($part, $part); } // Each section gets rules that depend on its own type. foreach ($this->list($plan, 'pages') as $pageIndex => $page) { foreach ($this->list($page, 'sections') as $sectionIndex => $section) { $type = is_array($section) ? ($section['type'] ?? null) : null; if (is_string($type) && isset(PlanSchema::pageSections()[$type])) { $rules += $this->sectionRules($type, "pages.$pageIndex.sections.$sectionIndex"); } } } /** @var array $clean */ $clean = Validator::make($plan, $rules)->validate(); $this->assertSectionIdsAreUnique($clean); $this->assertInternalLinksExist($clean); return $clean; } /** * Whether the plan is valid, without throwing. * * @param array $plan */ public function passes(array $plan): bool { try { $this->validate($plan); return true; } catch (ValidationException) { return false; } } /** * The variant and content rules for one section, placed at the given path in the plan. * * @return array> */ private function sectionRules(string $type, string $path): array { $rules = ["$path.variant" => ['required', Rule::in(PlanSchema::SECTIONS[$type])]]; foreach (PlanSchema::contentRules($type) as $field => $fieldRules) { $rules["$path.content.$field"] = array_map( fn (string $rule) => $this->qualify($rule, "$path.content."), $fieldRules, ); } return $rules; } /** * A section id must be unique across the whole plan, so links and chat edits can name one section. * * @param array $plan * * @throws ValidationException */ private function assertSectionIdsAreUnique(array $plan): void { $seen = []; foreach ($plan['pages'] as $pageIndex => $page) { foreach ($page['sections'] as $sectionIndex => $section) { if (isset($seen[$section['id']])) { throw ValidationException::withMessages([ "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.", ]); } $seen[$section['id']] = true; } } } /** * A link such as "/about" must point to a page of this plan or to one of the store's own pages. * This catches a menu link to a page that was never created or was removed. * * @param array $plan * * @throws ValidationException */ private function assertInternalLinksExist(array $plan): void { $known = [...array_column($plan['pages'], 'id'), ...PlanSchema::BUILT_IN_PATHS]; $check = function (mixed $value, string $path) use (&$check, $known): void { if (! is_array($value)) { return; } foreach ($value as $key => $item) { $itemPath = $path === '' ? (string) $key : $path.'.'.$key; if ($key === 'href' && is_string($item) && str_starts_with($item, '/') && $item !== '/') { $first = explode('/', ltrim(strtok($item, '?#') ?: '', '/'))[0]; if (! in_array($first, $known, true)) { throw ValidationException::withMessages([ $itemPath => "The link [$item] points to a page that does not exist. Use the id of a page in this plan, or /shop.", ]); } } $check($item, $itemPath); } }; $check($plan, ''); } /** * @return array */ private function list(mixed $value, string $key): array { return is_array($value) && is_array($value[$key] ?? null) ? $value[$key] : []; } /** * Rules such as "required_with:cta" or "required_if:items.*.kind,custom" name another field. * Point them at the full path. */ private function qualify(string $rule, string $prefix): string { foreach (['required_with:', 'required_if:'] as $name) { if (str_starts_with($rule, $name)) { return $name.$prefix.substr($rule, strlen($name)); } } return $rule; } }