SPB Git

Repository Public

validate.ts 7.5 KB 173 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : agentilab-web8 * File             : lib/catalog/validate.ts9 * Module           : Catalog10 * Repository       : https://git.spboucher.ai/agentilab-web11 * Project Website  : Not specified12 * Created          : 2026-12-0513 * Last Modified    : 2026-12-0514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : Proprietary34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { locales } from '@/lib/i18n/config';44import type { Localized } from '@/lib/i18n/config';45import type { Brand, ContactChannel, ImageAsset, Milestone, Product, Technology } from './types';4647const STATUSES = ['available', 'beta', 'development', 'unverified'];48const PLATFORMS = ['web', 'macos', 'ios', 'ipados'];49const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;50const SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;51/** Champs qui ne doivent jamais apparaître dans des données publiques. */52const SENSITIVE_KEYS = /address|adresse|shareholder|actionnaire|vote|phone|telephone|password|secret|token/i;5354function isHttps(url: string): boolean {55  try {56    return new URL(url).protocol === 'https:';57  } catch {58    return false;59  }60}6162function checkLocalized(value: Localized | null | undefined, path: string, errors: string[]): void {63  if (!value) {64    errors.push(`${path} : texte manquant`);65    return;66  }67  for (const locale of locales) {68    if (typeof value[locale] !== 'string' || value[locale].trim() === '') {69      errors.push(`${path}.${locale} : traduction manquante`);70    }71  }72}7374function checkImage(image: ImageAsset, path: string, errors: string[], altRequired: boolean): void {75  if (!image.src.startsWith('/')) errors.push(`${path}.src : doit être un fichier local (pas de hotlink)`);76  if (!(image.width > 0 && image.height > 0)) errors.push(`${path} : dimensions explicites requises`);77  if (altRequired) checkLocalized(image.alt, `${path}.alt`, errors);78}7980function checkSensitive(value: unknown, path: string, errors: string[]): void {81  if (!value || typeof value !== 'object') return;82  for (const [key, child] of Object.entries(value)) {83    if (SENSITIVE_KEYS.test(key)) errors.push(`${path}.${key} : champ sensible interdit dans les données publiques`);84    checkSensitive(child, `${path}.${key}`, errors);85  }86}8788export interface CatalogInput {89  brands: Brand[];90  products: Product[];91  technologies: Technology[];92  milestones: Milestone[];93  channels: ContactChannel[];94}9596/** Renvoie la liste des erreurs ; vide si le catalogue est publiable. */97export function validateCatalog(input: CatalogInput): string[] {98  const errors: string[] = [];99  const brandIds = new Set<string>();100101  for (const brand of input.brands) {102    const path = `brand[${brand.id}]`;103    if (brandIds.has(brand.id)) errors.push(`${path} : identifiant en double`);104    brandIds.add(brand.id);105    for (const key of ['tagline', 'summary', 'description', 'officialUrl'] as const) {106      checkLocalized(brand[key], `${path}.${key}`, errors);107    }108    for (const locale of locales) {109      if (!isHttps(brand.officialUrl[locale])) errors.push(`${path}.officialUrl.${locale} : URL HTTPS requise`);110    }111    if (!ISO_DATE.test(brand.verifiedAt)) errors.push(`${path}.verifiedAt : date AAAA-MM-JJ requise`);112    if (brand.screenshot) checkImage(brand.screenshot, `${path}.screenshot`, errors, true);113    checkSensitive(brand, path, errors);114  }115116  const ids = new Set<string>();117  const slugs = new Set<string>();118  for (const product of input.products) {119    const path = `product[${product.id}]`;120    if (ids.has(product.id)) errors.push(`${path} : identifiant en double`);121    ids.add(product.id);122    if (!SLUG.test(product.slug)) errors.push(`${path}.slug : format invalide`);123    if (slugs.has(product.slug)) errors.push(`${path}.slug : « ${product.slug} » en double`);124    slugs.add(product.slug);125    if (!brandIds.has(product.brandId)) errors.push(`${path}.brandId : marque « ${product.brandId} » inconnue`);126    if (!STATUSES.includes(product.status)) errors.push(`${path}.status : valeur inconnue`);127    if (product.publish && product.status === 'unverified') {128      errors.push(`${path} : publish=true interdit avec le statut « unverified »`);129    }130    if (product.platforms.some((p) => !PLATFORMS.includes(p))) errors.push(`${path}.platforms : valeur inconnue`);131    checkSensitive(product, path, errors);132    if (!product.publish) continue; // Les entrées non publiées n'ont pas à être complètes.133134    if (product.platforms.length === 0) errors.push(`${path}.platforms : au moins une plateforme`);135    for (const key of ['name', 'summary', 'description', 'officialUrl'] as const) {136      checkLocalized(product[key], `${path}.${key}`, errors);137    }138    for (const locale of locales) {139      if (!isHttps(product.officialUrl[locale])) errors.push(`${path}.officialUrl.${locale} : URL HTTPS requise`);140    }141    if (!ISO_DATE.test(product.verifiedAt)) errors.push(`${path}.verifiedAt : date AAAA-MM-JJ requise`);142    if (!product.evidenceRef) errors.push(`${path}.evidenceRef : preuve requise pour publier`);143    for (const link of product.storeLinks) {144      if (!isHttps(link.url)) errors.push(`${path}.storeLinks : URL HTTPS requise`);145      if (!ISO_DATE.test(link.verifiedAt)) errors.push(`${path}.storeLinks : date de vérification requise`);146    }147    if (product.icon) checkImage(product.icon, `${path}.icon`, errors, false);148    product.screenshots.forEach((shot, i) => checkImage(shot, `${path}.screenshots[${i}]`, errors, true));149    if (product.privacyNote) checkLocalized(product.privacyNote, `${path}.privacyNote`, errors);150  }151152  for (const tech of input.technologies) {153    for (const key of ['title', 'text', 'detail'] as const) checkLocalized(tech[key], `technology[${tech.id}].${key}`, errors);154    if (!tech.evidenceRef) errors.push(`technology[${tech.id}].evidenceRef : preuve requise`);155  }156157  for (const milestone of input.milestones) {158    const path = `milestone[${milestone.date}]`;159    if (!ISO_DATE.test(milestone.date)) errors.push(`${path} : date AAAA-MM-JJ requise`);160    checkLocalized(milestone.title, `${path}.title`, errors);161    checkLocalized(milestone.text, `${path}.text`, errors);162    if (!milestone.evidenceRef) errors.push(`${path}.evidenceRef : un jalon ne se publie que sur preuve`);163  }164165  for (const channel of input.channels) {166    if (channel.verified && !channel.verifiedAt) {167      errors.push(`channel[${channel.id}] : un canal vérifié doit porter sa date de vérification`);168    }169  }170171  return errors;172}173
Files main
Menu

Theme