Skip to content

Form

Byte includes utilities for fast form parsing.

import { form } from '@bit-js/byte';
// Retrieve a parameter value from the query
const parse = form.get('name', schema);
// Pass in the request context
// If schema type is string -> return string or null if the value is not found
// If schema type is number -> return number or NaN if the value is not found or invalid
// If schema type is boolean -> return true if the key is presented in the form and false otherwise
parse(ctx);
// Retrieve multiple key values from the form
const parseAll = form.schema({
name: nameSchema,
age: ageSchema,
...
});
// Return an object with all provided properties or null if any of
// the property does not match the corresponding schema
parseAll(ctx);

A form schema is as follows:

interface Schema {
// The type of the parsed value
type: 'number' | 'string' | 'bool' | 'file';
// Whether to get multiple values of the property
multipleItems?: boolean;
}

Some example schemas:

const multipleStringSchema = {
type: 'string',
multipleItems: true
};
const numberSchema = {
type: 'number'
};