typeofweb-org / schema

@typeofweb/schema: Lightweight validator with 100% TypeScript support and sane coercion rules.
https://schema.typeofweb.com
MIT License
139 stars 8 forks source link

Create resolver for react-hook-form #59

Open darkowic opened 3 years ago

darkowic commented 3 years ago

React hook form is a popular form validation library that supports forms schema resolving by resolver API (https://github.com/react-hook-form/resolvers). Resolvers are already implemented for validators like yup, superstruct, etc which are natural competitors for this library.

For me, it seems like a common use-case to use schema definitions to validate forms. But to do that we need to first support reporting errors per object attribute (#13).

Resolver draft (trying to do it here):

export const schemaResolver = (schema) => {
  const validator = validate(schema);

  return (values) => {
    try {
      return {
        values: validator(values),
        errors: {}
      };
    } catch (e) {
      const errors = /* get errors per schema attribute, each error should be a string */

      return {
        values: {},
        errors: errors
      };
    }
  };
}
typeofweb commented 3 years ago

Blocked by #13