1.109.0 • Published 10 months ago

@formdata/parser v1.109.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@formdata/parser

A specialized parser for Form-Data text-based form definitions. This package converts the .fd format into structured JSON that can be used to render forms.

Installation

npm install @formdata/parser

Features

  • 📋 Simple Syntax: Parse the human-readable .fd format
  • 🧩 Form Structure: Convert form definitions into structured objects
  • ⚠️ Error Handling: Detailed error reporting for validation
  • 🔍 Diagnostics: Editor-compatible diagnostics for IDEs
  • 📐 Extensible: Supports numerous input types and validations

Usage

import { parseFormDefinition } from '@formdata/parser';

// Form definition in .fd format
const formContent = `
type form
name Contact form
action https://example.com/submit
message Thank you for your message!
design
    theme light
    background #f5f5f5

type h1
text Contact Us

type text
name full_name
label Full Name
placeholder Enter your name
validations required|length:3,50

type email
name email_address
label Email Address
placeholder you@example.com
validations required|email

type submit
label Send Message
`;

// Parse the definition
const result = parseFormDefinition(formContent);

console.log(result.name); // "Contact form"
console.log(result.endpoint); // "https://example.com/submit"
console.log(result.blocks); // Array of form elements
console.log(result.errors); // Array of any parsing errors

Form Definition Format

The .fd format uses a simple syntax with indentation for nesting:

Basic Structure

type <block_type>
property1 value1
property2 value2
nested_property
    key1 value1
    key2 value2

Example Form

type form
name my_contact_form
action https://example.com/submit
message Thank you for contacting us!
design
    theme light
    background #f5f5f5

type h1
text Contact Us

type text
name full_name
label Full Name
placeholder Enter your full name
validations required|length:3,50

type email
name email
label Email Address
placeholder example@domain.com
validations required|email

type submit
label Send Message

Supported Block Types

Block TypeDescription
formThe root form element
h1 to h6Heading elements
textText input field
emailEmail input field
telTelephone input field
dateDate input field
textareaMultiline text area
selectDropdown select field
radioRadio button group
checkboxSingle checkbox
checkboxGroupGroup of checkboxes
fileFile upload field
sliderRange slider input
ratingStar rating input
buttonGeneric button
submitSubmit button

Common Properties

Most form elements support these properties:

PropertyDescription
nameField name (used in form submission)
labelDisplay label for the field
placeholderPlaceholder text
validationsValidation rules separated by pipe (\|)
defaultDefault value
helpHelp text displayed below the field
attributesCustom HTML attributes

Indented Properties

Some properties use indentation for nested values:

Design Settings

type form
name my_form
design
    theme dark
    background #333333

Options for Select, Radio, and CheckboxGroup

type select
name fruit
label Choose a fruit
options
    Apple
        value apple
    Banana
        value banana
    Orange
        value orange

Validation Rules

Validation rules are specified using a pipe-separated format:

validations required|length:3,50|email

Common validation rules:

  • required: Field must not be empty
  • email: Must be a valid email
  • length:min,max: Text length constraints
  • min:value: Minimum value (for numeric inputs)
  • max:value: Maximum value (for numeric inputs)
  • matches:/regex/: Custom regex pattern

Error Handling

The parser returns detailed error information:

const result = parseFormDefinition(formContent);

if (result.errors.length > 0) {
  result.errors.forEach(error => {
    console.log(`Line ${error.line}: ${error.message} (${error.code})`);
  });
}

Error codes:

  • PROPERTY_NOT_SUPPORTED: Property not supported for the block type
  • MISSING_REQUIRED_PROPERTY: Missing a required property
  • INVALID_INDENTATION: Incorrect indentation structure
  • INVALID_BLOCK_TYPE: Unknown block type
  • INVALID_PROPERTY_VALUE: Invalid property value
  • DUPLICATE_PROPERTY: Property defined multiple times

Editor Integration

The parser includes utilities for IDE/editor integration:

import { createDiagnostics } from '@formdata/parser';

// Create CodeMirror-compatible diagnostics
const diagnostics = createDiagnostics(formContent, result.errors);

API Reference

Main Functions

  • parseFormDefinition(text): Parse a form definition into a structured object
  • createDiagnostics(text, errors): Create editor-compatible diagnostics

Constants

  • ERROR_CODES: Enumeration of error code constants
  • PROPERTY_TYPES: Property type classifications
  • BLOCK_TYPES: Supported form block types
  • PROPERTIES: Property configurations for each block type
  • ALL_PROPERTY_NAMES: List of all valid property names

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

1.109.0

10 months ago

1.107.0

10 months ago

1.105.0

11 months ago

1.104.0

11 months ago

1.102.0

11 months ago

1.101.0

11 months ago

1.100.0

11 months ago

1.98.0

11 months ago

1.97.0

11 months ago

1.95.0

11 months ago

1.93.0

11 months ago

1.91.0

12 months ago

1.87.0

12 months ago

1.85.0

12 months ago

1.83.0

12 months ago

1.81.0

12 months ago

1.79.0

12 months ago

1.77.0

12 months ago

1.75.0

12 months ago

1.73.0

12 months ago

1.71.0

12 months ago

1.69.0

12 months ago

1.66.0

12 months ago

1.63.0

12 months ago

1.59.0

12 months ago

1.57.0

12 months ago

1.56.0

12 months ago

1.51.0

12 months ago

1.46.0

12 months ago

1.41.0

12 months ago

1.39.0

12 months ago

1.37.0

12 months ago

1.35.0

12 months ago

1.33.0

12 months ago

1.31.0

12 months ago

1.25.0

12 months ago

1.24.0

12 months ago

1.22.0

12 months ago

1.20.0

12 months ago

1.10.0

12 months ago

1.8.0

12 months ago

1.6.0

12 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago