{"version":3,"file":"masterteam-components-field-validation.mjs","sources":["../../../../packages/masterteam/components/field-validation/error-message.pipe.ts","../../../../packages/masterteam/components/field-validation/field-validation.ts","../../../../packages/masterteam/components/field-validation/character-limit.validator.ts","../../../../packages/masterteam/components/field-validation/masterteam-components-field-validation.ts"],"sourcesContent":["import { inject, Pipe, PipeTransform } from '@angular/core';\r\nimport { ValidationErrors } from '@angular/forms';\r\nimport { TranslocoService } from '@jsverse/transloco';\r\n\r\n@Pipe({\r\n  name: 'errorMessage',\r\n  standalone: true,\r\n  pure: false,\r\n})\r\nexport class ErrorMessagePipe implements PipeTransform {\r\n  private readonly translocoService = inject(TranslocoService);\r\n\r\n  transform(errors: ValidationErrors | null): string {\r\n    if (!errors) {\r\n      return '';\r\n    }\r\n\r\n    const t = (key: string, params?: object) =>\r\n      this.translocoService.translate(`components.validation.${key}`, params);\r\n\r\n    // A `.message` on the error object means a caller deliberately configured\r\n    // field-specific copy for this failure — ValidatorConfig with an explicit message\r\n    // (buildValidators → wrapValidatorWithMessage), or ValidatorConfig.custom\r\n    // (createCustomValidator). It must win over the generic translations below,\r\n    // otherwise that copy is silently replaced by the generic string, e.g. every\r\n    // pattern mismatch reading \"The format is invalid\" no matter what the field\r\n    // configured.\r\n    //\r\n    // The inverse matters just as much: ValidatorConfig's type-specific factories\r\n    // deliberately do NOT default `message` (see the note on ValidatorConfig in\r\n    // dynamic-form.model.ts). With no message configured the raw Angular validator is\r\n    // pushed unwrapped, so nothing reaches this loop and the translated — and, where\r\n    // relevant, parameter-interpolated — generic branch below runs instead. Re-adding\r\n    // a default there would shadow those translations in every locale but English.\r\n    for (const key in errors) {\r\n      const errorValue = errors[key];\r\n      if (errorValue && typeof errorValue === 'object' && errorValue.message) {\r\n        return errorValue.message;\r\n      }\r\n    }\r\n\r\n    if (errors['required']) {\r\n      return t('required');\r\n    }\r\n    if (errors['email']) {\r\n      return t('email');\r\n    }\r\n    if (errors['minlength']) {\r\n      const { requiredLength } = errors['minlength'];\r\n      return t('minLength', { requiredLength });\r\n    }\r\n    if (errors['maxlength']) {\r\n      const { requiredLength } = errors['maxlength'];\r\n      return t('maxLength', { requiredLength });\r\n    }\r\n    if (errors['characterLimit']) {\r\n      const { requiredLength } = errors['characterLimit'];\r\n      return t('maxLength', { requiredLength });\r\n    }\r\n    if (errors['min']) {\r\n      const { min } = errors['min'];\r\n      return t('min', { min });\r\n    }\r\n    if (errors['max']) {\r\n      const { max } = errors['max'];\r\n      return t('max', { max });\r\n    }\r\n    if (errors['pattern']) {\r\n      return t('pattern');\r\n    }\r\n    if (errors['fileSizeLimited']) {\r\n      const { size } = errors['fileSizeLimited'] ?? {};\r\n      return t('fileSizeLimited', { size });\r\n    }\r\n\r\n    // Fallback for any other error\r\n    const firstErrorKey = Object.keys(errors)[0];\r\n    return t('invalid', { key: firstErrorKey });\r\n  }\r\n}\r\n","import {\r\n  booleanAttribute,\r\n  Component,\r\n  computed,\r\n  DoCheck,\r\n  ElementRef,\r\n  inject,\r\n  input,\r\n} from '@angular/core';\r\nimport { Message } from 'primeng/message';\r\nimport { AbstractControl } from '@angular/forms';\r\nimport { ErrorMessagePipe } from './error-message.pipe';\r\nimport { _IdGenerator } from '@angular/cdk/a11y';\r\nimport { isInvalid } from '@masterteam/components';\r\n\r\n/**\r\n * First focusable thing inside a field host — the element a screen reader is on\r\n * when it needs to hear that the value was rejected. Covers the native controls\r\n * and the inner input every PrimeNG wrapper renders.\r\n */\r\nconst CONTROL_SELECTOR =\r\n  'input, textarea, select, [contenteditable=\"true\"], [role=\"combobox\"], [role=\"listbox\"], [role=\"spinbutton\"]';\r\n\r\n@Component({\r\n  selector: 'mt-field-validation',\r\n  standalone: true,\r\n  imports: [Message, ErrorMessagePipe],\r\n  template: `\r\n    @if (isInvalid(control())) {\r\n      <p-message\r\n        severity=\"error\"\r\n        variant=\"simple\"\r\n        size=\"small\"\r\n        role=\"alert\"\r\n        [id]=\"resolvedMessageId()\"\r\n        [text]=\"control().errors | errorMessage\"\r\n      >\r\n      </p-message>\r\n    }\r\n  `,\r\n  host: {\r\n    class: 'mt-field-validation',\r\n    // Reserve one line of space for fields that can produce an error so the\r\n    // message appears/disappears without shifting the field height (and the\r\n    // rest of the form). Fields with no validators stay compact.\r\n    '[class.mt-field-validation--reserved]': 'shouldReserveSpace()',\r\n  },\r\n  styles: `\r\n    .mt-field-validation {\r\n      display: none;\r\n    }\r\n    .mt-field-validation--reserved {\r\n      display: block;\r\n      min-height: 1.25rem;\r\n    }\r\n  `,\r\n})\r\nexport class FieldValidation implements DoCheck {\r\n  private readonly elementRef = inject<ElementRef<HTMLElement>>(ElementRef);\r\n\r\n  readonly control = input<AbstractControl | null>(null);\r\n  readonly touched = input<boolean, unknown>(false, {\r\n    transform: booleanAttribute,\r\n  });\r\n\r\n  /**\r\n   * Id the failing control points at with `aria-describedby`. Generated when\r\n   * not supplied, so no caller has to pass anything for the wiring below to\r\n   * work; override it only when a host needs a predictable id (a test, or a\r\n   * second `aria-describedby` target it is composing itself).\r\n   */\r\n  readonly messageId = input<string | null>(null);\r\n\r\n  private readonly fallbackMessageId =\r\n    inject(_IdGenerator).getId('mt-field-error-');\r\n\r\n  readonly resolvedMessageId = computed(\r\n    () => this.messageId()?.trim() || this.fallbackMessageId,\r\n  );\r\n\r\n  isInvalid = isInvalid;\r\n\r\n  /** Cached lookup — the control element never changes for a given field. */\r\n  private controlElement: HTMLElement | null | undefined;\r\n  private describedApplied = false;\r\n\r\n  /**\r\n   * Keep `aria-invalid` / `aria-describedby` on the field's own control in step\r\n   * with the message below it.\r\n   *\r\n   * The message used to be loose text with no relationship to anything: a\r\n   * screen-reader user heard the field, then — separately, later, if they kept\r\n   * reading — an unattributed error (findings C-02, H-04). Wiring it here rather\r\n   * than in each of the 23 field templates means every field that already\r\n   * renders `mt-field-validation` gets it, including the ones whose control is\r\n   * an input PrimeNG renders and the template cannot reach.\r\n   *\r\n   * `ngDoCheck` rather than an `effect` because `AbstractControl` validity is\r\n   * not a signal — the template re-reads it every cycle, and so does this.\r\n   */\r\n  ngDoCheck(): void {\r\n    const target = this.resolveControlElement();\r\n    if (!target) {\r\n      return;\r\n    }\r\n\r\n    const invalid = isInvalid(this.control());\r\n    if (invalid === this.describedApplied) {\r\n      return;\r\n    }\r\n\r\n    this.describedApplied = invalid;\r\n\r\n    if (invalid) {\r\n      target.setAttribute('aria-invalid', 'true');\r\n      target.setAttribute('aria-describedby', this.resolvedMessageId());\r\n      return;\r\n    }\r\n\r\n    target.removeAttribute('aria-invalid');\r\n    if (target.getAttribute('aria-describedby') === this.resolvedMessageId()) {\r\n      target.removeAttribute('aria-describedby');\r\n    }\r\n  }\r\n\r\n  /**\r\n   * Reserve the error-message line whenever the control can surface an error —\r\n   * i.e. it has sync/async validators, or is already showing one. This keeps the\r\n   * field's height constant so showing the error never pushes sibling fields or\r\n   * the content below the row. Re-evaluated each change-detection cycle, so it\r\n   * picks up validators that fields add asynchronously after init.\r\n   */\r\n  shouldReserveSpace(): boolean {\r\n    const control = this.control();\r\n    if (!control) {\r\n      return false;\r\n    }\r\n    return (\r\n      control.validator !== null ||\r\n      control.asyncValidator !== null ||\r\n      isInvalid(control)\r\n    );\r\n  }\r\n\r\n  /**\r\n   * The control this message belongs to: the first focusable element inside the\r\n   * field component that hosts us. `undefined` means \"not looked up yet\",\r\n   * `null` means \"looked up and there is nothing to annotate\" — a distinction\r\n   * that matters, because the second case must not re-query every cycle.\r\n   */\r\n  private resolveControlElement(): HTMLElement | null {\r\n    if (this.controlElement !== undefined) {\r\n      return this.controlElement;\r\n    }\r\n\r\n    const host = this.elementRef.nativeElement.parentElement;\r\n    this.controlElement =\r\n      host?.querySelector<HTMLElement>(CONTROL_SELECTOR) ?? null;\r\n\r\n    return this.controlElement;\r\n  }\r\n}\r\n","import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';\n\nexport const characterLimitValidator =\n  (maxLength: number): ValidatorFn =>\n  (control: AbstractControl): ValidationErrors | null => {\n    const value = control.value;\n\n    if (value === null || value === undefined) {\n      return null;\n    }\n\n    const length = String(value).length;\n\n    return length > maxLength\n      ? {\n          characterLimit: {\n            requiredLength: maxLength,\n            actualLength: length,\n          },\n        }\n      : null;\n  };\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;;;MASa,gBAAgB,CAAA;AACV,IAAA,gBAAgB,GAAG,MAAM,CAAC,gBAAgB,CAAC;AAE5D,IAAA,SAAS,CAAC,MAA+B,EAAA;QACvC,IAAI,CAAC,MAAM,EAAE;AACX,YAAA,OAAO,EAAE;QACX;QAEA,MAAM,CAAC,GAAG,CAAC,GAAW,EAAE,MAAe,KACrC,IAAI,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAA,sBAAA,EAAyB,GAAG,CAAA,CAAE,EAAE,MAAM,CAAC;;;;;;;;;;;;;;;AAgBzE,QAAA,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE;AACxB,YAAA,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC;YAC9B,IAAI,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,UAAU,CAAC,OAAO,EAAE;gBACtE,OAAO,UAAU,CAAC,OAAO;YAC3B;QACF;AAEA,QAAA,IAAI,MAAM,CAAC,UAAU,CAAC,EAAE;AACtB,YAAA,OAAO,CAAC,CAAC,UAAU,CAAC;QACtB;AACA,QAAA,IAAI,MAAM,CAAC,OAAO,CAAC,EAAE;AACnB,YAAA,OAAO,CAAC,CAAC,OAAO,CAAC;QACnB;AACA,QAAA,IAAI,MAAM,CAAC,WAAW,CAAC,EAAE;YACvB,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC;YAC9C,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE,cAAc,EAAE,CAAC;QAC3C;AACA,QAAA,IAAI,MAAM,CAAC,WAAW,CAAC,EAAE;YACvB,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC;YAC9C,O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