Parser
Parse generated form config without running generated code.
Use the parser between AI output, migration text, or builder exports and the Formedible field model your app reviews and renders.
Public exports
Import parser pieces from the package root. It exports FormedibleParser, fence extraction, field type metadata, parser types, and config helpers.
- FormedibleParser and extractFormedibleCode come from lib/formedible/formedible-parser.
- ParserConfig, defaultParserConfig, mergeParserConfig, validateParserConfig, and generateSystemPrompt come from parser-config-schema.
- FormedibleParseResult records success, config, code, source, and errors.
Main API
FormedibleParser has throwing methods for direct parser work and result-returning methods for chat or editor flows.
- parse rejects empty strings, oversized strings, executable syntax, invalid object syntax, and invalid config shape.
- parseStructured accepts the chosen structured candidate, clones serializable values, then validates and sanitizes.
- parseAiOutput returns success false instead of throwing when extraction or parsing fails.
- parseWithSchemaInference and mergeSchemas are static methods on the same class.
| Property | Type | Default | Description |
|---|---|---|---|
parse | (code: string, options?: ParserOptions | EnhancedParserOptions) => ParsedFormConfig | Throws on error | Parses JSON or object-literal config text after executable syntax checks and sanitization. |
parseStructured | (output: FormedibleStructuredOutput, options?: ParserOptions | EnhancedParserOptions) => ParsedFormConfig | Throws on error | Clones serializable structured output, selects the likely config payload, then validates it. |
parseAiOutput | (output: string | FormedibleStructuredOutput, options?: ParserOptions | EnhancedParserOptions) => FormedibleParseResult | Result object | Handles structured output, lowercase formedible fences, and direct object text with non-throwing errors. |
parseWithSchemaInference | (code: string, options?: SchemaInferenceOptions) => SchemaInferenceResult | confidence 0 to 1 | Parses config and, when enabled, builds an inferred schema shape from field types and min or max hints. |
mergeSchemas | (parsedConfig, baseSchema, strategy?) => UseFormedibleOptions | extend | Combines parsed fields with an existing schema using extend, override, or intersect. |
validateWithSuggestions | (code: string) => ValidationWithSuggestionsResult | Result object | Runs parse and returns errors plus suggestions instead of throwing. |
Extraction
extractFormedibleCode only accepts a lowercase formedible fence. Other common code fences return a validation error.
- A matching fence returns source fenced and the trimmed block body.
- json, ts, tsx, typescript, javascript, and js fences return source none with a validation error.
- parseAiOutput handles direct object text after extraction returns no code and no errors.
Security
The parser rejects executable syntax before object parsing. It then sanitizes config keys and values before returning ParsedFormConfig.
- executableSyntaxPattern includes arrow functions, function declarations, classes, constructor calls, eval, Function, timers, require, dynamic imports, and capitalized JSX-like markup.
- sanitizePlainConfig rejects component, render, children, onChange, onBlur, onFocus, onSubmit, and conditional keys.
- sanitizeField rejects unsupported field keys and unsupported field types.
- validateAndSanitize rejects unsupported top-level keys when strictValidation is true.
Parser configuration
ParserConfig is the settings object for parser screens, schema validation, and parser behavior.
- strictValidation controls whether unsupported top-level keys fail or get skipped.
- enableSchemaInference toggles inferred schema output from parsed fields.
- mergeStrategy sets the default schema merge behavior: extend, override, or intersect.
- maxCodeLength protects parser work from oversized responses.
- enableZodParsing is a parser config/schema/form field that allows inert Zod expression syntax in form definitions.
| Property | Type | Default | Description |
|---|---|---|---|
strictValidation | boolean | true | Reject unsupported top-level keys instead of skipping them. |
enableSchemaInference | boolean | false | Build inferred schema metadata from parsed fields when requested. |
mergeStrategy | 'extend' | 'override' | 'intersect' | 'extend' | Select the default strategy used when parsed config meets a base schema. |
maxCodeLength | number | 1000000 | Maximum config text length accepted by parser settings. |
enableZodParsing | boolean | true | Configuration field for allowing inert Zod expression syntax in form definitions. |
defaultParserConfig | ParserConfig | Exported value | Default settings used by parser configuration forms and parser behavior. |
Schema inference
Schema inference maps parsed fields to Zod-style strings only when options.enabled is true. mergeSchemas combines parsed fields with baseSchema.properties by the selected strategy.
- Text and textarea fields can add min or max string checks from field limits.
- Number and slider fields can add min or max numeric checks from field limits.
- A field with required set to false becomes optional in the inferred schema text.
- mergeSchemas with extend keeps parsed fields and adds missing fields from baseSchema.properties.
- mergeSchemas with override keeps parsed fields but replaces schema with the base schema.
- mergeSchemas with intersect keeps only parsed fields that also exist in baseSchema.properties.