domain-react-forms

React Forms generator for Domain Schema

Usage no npm install needed!

<script type="module">
  import domainReactForms from 'https://cdn.skypack.dev/domain-react-forms';
</script>

README

Domain Schema Auto React Forms

npm version Twitter Follow

Installation

npm install -g domain-react-forms

Usage

const userFormSchema = new DomainSchema(
  class User extends Schema {
    __ = { name: 'User' };
    id = DomainSchema.Int;
    username = {
      type: String,
      required: true,
      validators: [(value) => {
        return value.length > 3 ? undefined : 'Must Be more than 3 characters';
      }]
    };
    email = {
      type: String,
      required: true,
      email: true
    };
    profile = {
      type: Profile
    };
    password = {
      type: String,
      required: true,
      minLength: 5
    };
    passwordConfirmation = {
      type: String,
      required: true,
      minLength: 5,
      match: 'password'
    };
  }
);
class Profile extends Schema {
  __ = { name: 'Profile' };
  firstName = {
    type: String,
    required: {
      value: true,
      msg: 'Required First Name'
    }
  };
  lastName = {
      type: String,
      required: true
  };
};

const userForm =  new DomainReactForms(userFormSchema);

// change error messages
DomainReactForms.setValidationMessages({
  required: ({field}) => {
    return `Field '${field}' is required`
  }
});

const values = {
  // form values
};
const errors = userForm.validate(values);

License

Copyright © 2017 SysGears INC. This source code is licensed under the MIT license.