DEV Community
Follow
Your Form Is Not Portable If It Contains Callbacks
A truly portable form requires more than just JSON serialization; its validation, conditions, and submission logic must also be transferable. Standard form libraries often embed behavior within callbacks, making them non-portable and unsuitable for backend generation or multi-application use. Attempting to serialize callbacks as source code or invent compact DSLs introduces significant security and maintenance risks. Instead, behavior should be represented declaratively through a serializable expression tree. This approach allows for inspectable, versionable, and independently validatable data structures. While raw expression trees are verbose, a typed authoring API, like TypeScript builders, can abstract this complexity, generating a canonical JSON contract. This contract serves as a universal source of truth, enabling various frontends and backends to interpret and validate forms consistently. Crucially, positional collections need careful handling to avoid silently altering data identity upon submission. Portable form systems must also employ robust validation and security measures to handle untrusted input safely, preventing issues like catastrophic backtracking or invalid configurations. This architectural approach, explored by Modyra, shifts complexity from individual applications to shared infrastructure. It aims to ensure identical meaning across different runtimes, not necessarily identical rendering. The core challenge lies in determining how much behavior can be encoded as portable data without making the contract overly complex.