$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)], '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])], ]; 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); 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; } } } /** * @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" name another field. Point them at the full path. */ private function qualify(string $rule, string $prefix): string { if (str_starts_with($rule, 'required_with:')) { return 'required_with:'.$prefix.substr($rule, strlen('required_with:')); } return $rule; } }