Welcome testers! This document will give you a complete breakdown of @silentcoderhi/superform, what it does, where to use its native functions, and how to rigorously test its latest capabilities (v1.1.0).
SuperForm is a lightning-fast, zero-dependency, TypeScript-first declarative validation engine and React form-state management library. It acts as both your "strict gatekeeper" for defining data shapes (like Zod or Yup) and your robust state binder for React input elements (like react-hook-form or Formik).
SuperForm handles the heavy lifting of user inputs:
- Validates Data Schemas: Checks your strings, numbers, booleans, and arrays against precise constraints you define (lengths, emails, enums, literals, conditional logic).
- Manages React Form State: The
useForm()hook tracks whether forms are submitting, records live validation errors safely without triggering excessive re-renders, and wiresonChangeandvaluebindings natively for fields via the simple.register("fieldName")abstraction. - Coerces and Formats (v1.1.0+): It can actively intercept form data, strip characters, convert to numbers, or apply developer-defined
.transform()modifications instantly as it passes.
SuperForm logic is split into two halves: the Core Validation Engine and the React Hook Integration.
You use the superform object to draw a "Schema Map".
import { superform } from "@silentcoderhi/superform/core";
const mySchema = superform.object({
email: superform.string().email(),
score: superform.number().max(10)
});Here are all the functionalities you can build your schema with:
superform.string(): Base text. Chain.min(n),.max(n),.email()to add restrictions.superform.number(): Base numeric. Chain.min(n),.max(n)to add restrictions.superform.boolean(): Chain.true('Required!')to enforce a strictlytruevalue.superform.array(schema): Array containing other schemas.superform.object({ ... }): Defines an explicitly shaped dictionary mapping string keys to schemas.
superform.enum(["a", "b", "c"]): Forces the value to exclusively exist within the array.superform.literal("MAGIC"): Forces the value to strictly equal a known constant.superform.union(schema1, schema2): Allows the input to match safely against multiple schemas. Perfect for"string | number"toggles.
Modifiers can be wrapped onto the end of any of the schemas described above.
.optional(): The value may be explicitlyundefinedor skipped entirely without breaking the form..nullable(): The value may explicitly benull..refine(predicateFn, { message }): Supply a custom function. If the function returnsfalse, validation halts with your custom error message. Example: password complexity logic..transform(mappingFn): Coercion. Change an input completely on the fly dynamically (Example: Strip white spaces, or output integers from string characters).
You inject your mySchema into a React function component using the useForm hook so it has total control to read, lock, and inject errors onto the screen.
import { useForm } from "@silentcoderhi/superform/react";
function Sample() {
const { register, handleSubmit, errors, isSubmitting, reset } = useForm(mySchema);
const submitProcess = async (data) => {
console.log('Sanitized Payload ready for Server:', data);
}
return (
<form onSubmit={handleSubmit(submitProcess)}>
{/* Hooking the specific input tag up using the registered ID name! */}
<input {...register("email")} />
{errors.email && <span>{errors.email}</span>}
</form>
)
}register("fieldName"): Drops{ name, onChange, onBlur }native binders directly into an HTML<input>or<select>.handleSubmit(fn): A wrapper for your button trigger. It prevents native page reloads, forces a strict layout Schema Validation check against all fields, blocks the user on errors, and only firesfnif successful.errors: An object literal mirroring the exact structure of your schema mapped straight to validation string errors to conditionally draw red text below inputs.
As a tester receiving this package today, you should focus highly on stressing the v1.1.0 capabilities inside the playground test app:
- Transform Mutilations: Verify if a user types inputs with extra spaces into
.transform(v => v.trim()), theonSubmitultimately returns the pristine unspaced format. - Refine Edge Cases: Stress test specific
.refineconditions (like regex testing inside passports/IDs). Ensure the custom messages are consistently firing. - Empty Data Behaviors: See if nested
.optional()vs.nullable()fail properly on partial inputs versus wholly skipped ones. Try toggling an.optional()off manually and assertinguseFormtriggers an immediate localized UI.requiredfallback trap. - Union Crossings: Type numbers manually inside string inputs bounded by
superform.union(string, number)to confirm it successfully jumps between internal schema trees cleanly without collapsing the compiler.