ngx-api-forms 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +657 -0
- package/analog/index.d.ts +76 -0
- package/django/index.d.ts +56 -0
- package/express-validator/index.d.ts +60 -0
- package/fesm2022/ngx-api-forms-analog.mjs +195 -0
- package/fesm2022/ngx-api-forms-analog.mjs.map +1 -0
- package/fesm2022/ngx-api-forms-django.mjs +173 -0
- package/fesm2022/ngx-api-forms-django.mjs.map +1 -0
- package/fesm2022/ngx-api-forms-express-validator.mjs +202 -0
- package/fesm2022/ngx-api-forms-express-validator.mjs.map +1 -0
- package/fesm2022/ngx-api-forms-laravel.mjs +167 -0
- package/fesm2022/ngx-api-forms-laravel.mjs.map +1 -0
- package/fesm2022/ngx-api-forms-zod.mjs +226 -0
- package/fesm2022/ngx-api-forms-zod.mjs.map +1 -0
- package/fesm2022/ngx-api-forms.mjs +890 -0
- package/fesm2022/ngx-api-forms.mjs.map +1 -0
- package/index.d.ts +621 -0
- package/laravel/index.d.ts +49 -0
- package/package.json +85 -0
- package/schematics/collection.json +10 -0
- package/schematics/ng-add/index.js +300 -0
- package/schematics/ng-add/index.spec.js +119 -0
- package/schematics/ng-add/schema.json +34 -0
- package/zod/index.d.ts +58 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ngx-api-forms-zod.mjs","sources":["../../../projects/ngx-api-forms/zod/src/zod.preset.ts","../../../projects/ngx-api-forms/zod/src/ngx-api-forms-zod.ts"],"sourcesContent":["/**\n * Zod error preset.\n *\n * Parses errors from `ZodError.flatten()`:\n * ```json\n * {\n * \"formErrors\": [],\n * \"fieldErrors\": {\n * \"email\": [\"Invalid email\"],\n * \"name\": [\"String must contain at least 3 character(s)\"]\n * }\n * }\n * ```\n *\n * Also supports raw `ZodError.issues`:\n * ```json\n * [\n * { \"code\": \"too_small\", \"minimum\": 3, \"path\": [\"name\"], \"message\": \"...\" },\n * { \"code\": \"invalid_string\", \"validation\": \"email\", \"path\": [\"email\"], \"message\": \"...\" }\n * ]\n * ```\n */\nimport { ApiFieldError, ConstraintMap, ErrorPreset, GLOBAL_ERROR_FIELD, ZodFlatError } from 'ngx-api-forms';\n\ninterface ZodIssue {\n code: string;\n path: (string | number)[];\n message: string;\n minimum?: number;\n maximum?: number;\n validation?: string;\n}\n\n/**\n * Infers a constraint key from a Zod error message string.\n *\n * @remarks\n * Used only for the flattened format (`ZodError.flatten()`), where the structured\n * `code` field is not available. Relies on English-language pattern matching.\n * The raw issues format uses `zodCodeToConstraint` instead, which is language-independent.\n * If your Zod messages are customized or translated, prefer returning raw issues\n * (`ZodError.issues`) rather than the flattened format.\n */\n/**\n * Tries user-provided constraint patterns against a message.\n * Returns the matched constraint key or null.\n */\nfunction matchUserPatterns(message: string, patterns: Record<string, RegExp>): string | null {\n for (const [constraint, regex] of Object.entries(patterns)) {\n if (regex.test(message)) return constraint;\n }\n return null;\n}\n\nfunction inferConstraintFromMessage(message: string): string {\n const lower = message.toLowerCase();\n if (lower.includes('required') || lower.includes('invalid_type')) return 'required';\n if (lower.includes('email')) return 'email';\n if (lower.includes('url')) return 'url';\n if (lower.includes('at least') || lower.includes('too_small') || lower.includes('must contain at least')) return 'minlength';\n if (lower.includes('at most') || lower.includes('too_big') || lower.includes('must contain at most')) return 'maxlength';\n if (lower.includes('greater than or equal')) return 'min';\n if (lower.includes('less than or equal')) return 'max';\n return 'serverError';\n}\n\nfunction zodCodeToConstraint(issue: ZodIssue): string {\n switch (issue.code) {\n case 'too_small':\n return issue.minimum !== undefined ? 'minlength' : 'min';\n case 'too_big':\n return issue.maximum !== undefined ? 'maxlength' : 'max';\n case 'invalid_string':\n return issue.validation ?? 'serverError';\n case 'invalid_type':\n return 'required';\n case 'invalid_enum_value':\n return 'enum';\n case 'invalid_date':\n return 'date';\n case 'custom':\n return 'custom';\n default:\n return issue.code || 'serverError';\n }\n}\n\n/**\n * Default constraint map for Zod.\n */\nexport const ZOD_CONSTRAINT_MAP: ConstraintMap = {\n required: 'required',\n email: 'email',\n url: 'url',\n uuid: 'uuid',\n minlength: 'minlength',\n maxlength: 'maxlength',\n min: 'min',\n max: 'max',\n regex: 'pattern',\n enum: 'enum',\n date: 'date',\n custom: 'custom',\n invalid: 'invalid',\n serverError: 'serverError',\n};\n\n/**\n * Creates a Zod error preset.\n *\n * Supports both `.flatten()` and raw `.issues` formats.\n *\n * @param options.noInference - When true, skips constraint guessing entirely.\n * The raw error message is used directly and the constraint is set to `'serverError'`.\n * Useful for custom or translated Zod error messages.\n * @param options.constraintPatterns - Custom regex patterns for constraint inference.\n * Keys are constraint names, values are RegExp tested against the raw message.\n * Checked before the built-in English patterns (flattened format only;\n * the raw issues format uses structured `code` fields which are language-independent).\n *\n * @example\n * ```typescript\n * import { zodPreset } from 'ngx-api-forms/zod';\n *\n * const bridge = createFormBridge(form, { preset: zodPreset() });\n *\n * // No inference: raw messages, no guessing\n * const bridge = createFormBridge(form, { preset: zodPreset({ noInference: true }) });\n * ```\n */\nexport function zodPreset(options?: { noInference?: boolean; constraintPatterns?: Record<string, RegExp> }): ErrorPreset {\n const skipInference = options?.noInference ?? false;\n const userPatterns = options?.constraintPatterns;\n\n function inferMessage(message: string): string {\n if (userPatterns) {\n const match = matchUserPatterns(message, userPatterns);\n if (match) return match;\n }\n return inferConstraintFromMessage(message);\n }\n\n return {\n name: 'zod',\n constraintMap: ZOD_CONSTRAINT_MAP,\n parse(error: unknown): ApiFieldError[] {\n if (!error || typeof error !== 'object') return [];\n\n // Format 1: Flattened error { fieldErrors: { ... }, formErrors: [...] }\n const flat = error as Partial<ZodFlatError>;\n if (flat.fieldErrors && typeof flat.fieldErrors === 'object') {\n const result: ApiFieldError[] = [];\n\n // Collect form-level (global) errors\n if (Array.isArray(flat.formErrors)) {\n for (const message of flat.formErrors) {\n if (typeof message !== 'string') continue;\n result.push({ field: GLOBAL_ERROR_FIELD, constraint: 'serverError', message });\n }\n }\n\n for (const [field, messages] of Object.entries(flat.fieldErrors)) {\n if (!Array.isArray(messages)) continue;\n for (const message of messages) {\n if (typeof message !== 'string') continue;\n result.push({ field, constraint: skipInference ? 'serverError' : inferMessage(message), message });\n }\n }\n return result;\n }\n\n // Format 2: Raw issues array\n const err = error as Record<string, unknown>;\n if (Array.isArray(err['issues'])) {\n const issues = err['issues'] as ZodIssue[];\n return issues.map((issue) => ({\n field: issue.path && issue.path.length > 0\n ? issue.path.map(String).join('.')\n : GLOBAL_ERROR_FIELD,\n constraint: skipInference ? 'serverError' : zodCodeToConstraint(issue),\n message: issue.message,\n }));\n }\n\n // Format 3: Direct array of issues\n if (Array.isArray(error)) {\n const issues = error as ZodIssue[];\n if (issues.length > 0 && 'code' in issues[0] && 'path' in issues[0]) {\n return issues.map((issue) => ({\n field: issue.path && issue.path.length > 0\n ? issue.path.map(String).join('.')\n : GLOBAL_ERROR_FIELD,\n constraint: skipInference ? 'serverError' : zodCodeToConstraint(issue),\n message: issue.message,\n }));\n }\n }\n\n // Format 4: Wrapped { errors: { fieldErrors: {...}, formErrors: [...] } } or { error: {...} }\n if (err['errors'] && typeof err['errors'] === 'object') {\n const nested = err['errors'] as Partial<ZodFlatError>;\n if (nested.fieldErrors && typeof nested.fieldErrors === 'object') {\n const result: ApiFieldError[] = [];\n\n // Collect form-level (global) errors from wrapped format\n if (Array.isArray(nested.formErrors)) {\n for (const message of nested.formErrors) {\n if (typeof message !== 'string') continue;\n result.push({ field: GLOBAL_ERROR_FIELD, constraint: 'serverError', message });\n }\n }\n\n for (const [field, messages] of Object.entries(nested.fieldErrors)) {\n if (!Array.isArray(messages)) continue;\n for (const message of messages) {\n if (typeof message !== 'string') continue;\n result.push({ field, constraint: skipInference ? 'serverError' : inferMessage(message), message });\n }\n }\n return result;\n }\n }\n\n return [];\n },\n };\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;;;;;;;AAqBG;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