2.0.1 • Published 4 years ago

formook v2.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

Resume

Validation hook to deal with React forms

Designed to be used with a single object, allowing strong autocompletion

The validation trigger is debounced to optimize performances

npm i formook --save

API

Validation schemas are made using a mix of :

The validation will be triggered only when the object is updated

It forces you to update your object reference when you want to switch between schemas, but you should not need it as the dynamic schemas are made possible with functions

Note that Yup and Joi can't be used both at the same time, you need to use the function switchHighLevelValidation to define your schema library (yup by default)

function switchHighLevelValidation: (
  schemaType: HighLevelSchema
) => HighLevelSchema;

function useFormValidation: <T extends object>(
  schema: FormValidationSchema<T>,
  object: T,
  options?: FormValidationOptions
) => {
  canValidate: boolean;
  errors: FormValidationError<T> [];
};

Types

type HighLevelSchema = "yup" | "joi";

type FormValidationSchemaFunction<T extends object> = (
  object: T
) => boolean | string;

type FormValidationErrors<T extends object> =
  | {
      [K in keyof T]?: string;
    }
  // This one is needed because of joi errors
  | { [K: string]: string };

type FormValidationOptions = Yup.ValidateOptions | Joi.ValidationOptions;

Example

interface Test {
  foo: number;
  foo2: string;
}

const object: Test = await fetchData();

let schema: FormValidationSchema<Test> = {
  foo: Yup.number.max(10),
  foo2: (object: Test) =>
    object.foo2.length > 5 || "Input should be at least 6 chars long",
};

// If you are using joi

switchHighLevelValidation("joi");

schema = {
  foo: Joi.number.max(10),
};

const { canValidate, errors } = useFormValidation(schema, object);
2.0.1

4 years ago

2.0.0

4 years ago

1.9.6

4 years ago

1.9.5

4 years ago

1.9.4

4 years ago

1.9.3

4 years ago

1.9.2

4 years ago

1.9.1

4 years ago

1.9.0

4 years ago

1.8.7

4 years ago

1.8.6

4 years ago

1.8.5

4 years ago

1.8.4

4 years ago

1.8.3

4 years ago

1.8.2

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.9

4 years ago

1.7.8

4 years ago

1.7.7

4 years ago

1.7.6

4 years ago

1.7.5

4 years ago

1.6.4

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.6.9

4 years ago

1.6.6

4 years ago

1.7.4

4 years ago

1.6.5

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago