a340d8139a
Auto Tag Develop / tag (push) Successful in 8s
## Contexte ERP-148 — mise à jour @malio/layer-ui et amélioration des champs date (onglet Information, Client & Fournisseur). ## Changements - **MalioDate v1.7.10** : le composant expose désormais son état de validité (`@update:valid`) et la saisie brute invalide (`@update:rawValue`). - **Validation back-autoritaire du format** : `foundedAt` n'accepte plus que l'ISO strict `Y-m-d` (`#[Context]` DateTimeNormalizer) + `collectDenormalizationErrors` sur `Client` et `Supplier`. Toute saisie non-ISO renvoie un **422 porté sur le champ**. - Corrige un cas piège : `12/25/2026` (invalide en JJ/MM/AAAA côté front) était auparavant accepté par PHP en M/J/AAAA → 25 décembre. Désormais rejeté. - **Front** : la saisie invalide est transmise au back ; le message technique de type-error est surchargé par une clé i18n via le **code de violation** (`resolveViolationMessage` / `VIOLATION_MESSAGE_I18N`), affiché inline par `useFormErrors`. - Réorganisation des utils de formulaire sous `utils/forms/`. ## Tests - Back : `ClientFoundedAtFormatTest` / `SupplierFoundedAtFormatTest` (dont le cas piège `12/25/2026`). - Front : résolveur i18n (`api.test.ts`, `useFormErrors.test.ts`) + payloads (`clientEdit`/`supplierEdit` specs). - Suite Commercial verte ; vérifié bout-en-bout en navigateur (PATCH → 422, erreur inline, submit bloqué). ## Note Échecs JWT aléatoires connus du hook pre-commit (401/500 sur tests d'auth sans rapport) ; tous verts en isolation. Reviewed-on: #92 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
86 lines
3.5 KiB
TypeScript
86 lines
3.5 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { mapViolationsToRecord, resolveViolationMessage } from '../api'
|
|
|
|
/**
|
|
* Tests de `mapViolationsToRecord` — fondation du mapping erreur→champ des
|
|
* formulaires (ERP-101). Transforme un payload 422 API Platform en
|
|
* `Record<propertyPath, message>` directement consommable par la prop `:error`
|
|
* des composants `Malio*`.
|
|
*/
|
|
describe('mapViolationsToRecord', () => {
|
|
it('mappe chaque violation par son propertyPath (format `violations`)', () => {
|
|
const data = {
|
|
violations: [
|
|
{ propertyPath: 'companyName', message: 'Obligatoire.' },
|
|
{ propertyPath: 'siren', message: 'SIREN deja utilise.' },
|
|
],
|
|
}
|
|
expect(mapViolationsToRecord(data)).toEqual({
|
|
companyName: 'Obligatoire.',
|
|
siren: 'SIREN deja utilise.',
|
|
})
|
|
})
|
|
|
|
it('supporte le format negocie `hydra:violations`', () => {
|
|
const data = {
|
|
'hydra:violations': [
|
|
{ propertyPath: 'email', message: 'Adresse invalide.' },
|
|
],
|
|
}
|
|
expect(mapViolationsToRecord(data)).toEqual({ email: 'Adresse invalide.' })
|
|
})
|
|
|
|
it('renvoie un objet vide quand il n\'y a pas de violation exploitable', () => {
|
|
expect(mapViolationsToRecord({})).toEqual({})
|
|
expect(mapViolationsToRecord(null)).toEqual({})
|
|
expect(mapViolationsToRecord({ violations: [] })).toEqual({})
|
|
})
|
|
|
|
it('ignore les violations sans propertyPath', () => {
|
|
const data = {
|
|
violations: [
|
|
{ propertyPath: '', message: 'Erreur globale.' },
|
|
{ propertyPath: 'iban', message: 'IBAN invalide.' },
|
|
],
|
|
}
|
|
expect(mapViolationsToRecord(data)).toEqual({ iban: 'IBAN invalide.' })
|
|
})
|
|
|
|
it('en cas de doublon de propertyPath, la derniere violation gagne', () => {
|
|
const data = {
|
|
violations: [
|
|
{ propertyPath: 'name', message: 'Premier message.' },
|
|
{ propertyPath: 'name', message: 'Second message.' },
|
|
],
|
|
}
|
|
expect(mapViolationsToRecord(data)).toEqual({ name: 'Second message.' })
|
|
})
|
|
})
|
|
|
|
/**
|
|
* Tests de `resolveViolationMessage` — surcharge i18n d'un message back par code
|
|
* de violation. Le back peut renvoyer un message technique (erreur de type sur
|
|
* une date non parsable) : on le remplace via le `code` Symfony (stable) par une
|
|
* cle i18n, sans toucher au back. Le `t` ici renvoie la cle telle quelle.
|
|
*/
|
|
describe('resolveViolationMessage', () => {
|
|
const t = (key: string) => key
|
|
// Code Symfony Constraints\Type::INVALID_TYPE_ERROR (fige).
|
|
const TYPE_ERROR = 'ba785a8c-82cb-4283-967c-3cf342181b40'
|
|
|
|
it('surcharge le message technique d\'une erreur de type par la cle i18n', () => {
|
|
const v = { propertyPath: 'foundedAt', message: 'Cette valeur doit être de type DateTimeImmutable|null.', code: TYPE_ERROR }
|
|
expect(resolveViolationMessage(v, t)).toBe('errors.validation.invalidDate')
|
|
})
|
|
|
|
it('renvoie le message back tel quel quand le code n\'est pas surcharge', () => {
|
|
const v = { propertyPath: 'companyName', message: 'Le nom est obligatoire.', code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3' }
|
|
expect(resolveViolationMessage(v, t)).toBe('Le nom est obligatoire.')
|
|
})
|
|
|
|
it('renvoie le message back tel quel quand il n\'y a pas de code', () => {
|
|
const v = { propertyPath: 'siren', message: 'SIREN deja utilise.', code: '' }
|
|
expect(resolveViolationMessage(v, t)).toBe('SIREN deja utilise.')
|
|
})
|
|
})
|