valtors
TypeScript icon, indicating that this package has built-in type declarations

2.6.1 • Public • Published

Valtors npm package

Valtors is a small flexible and extensible validation library for TypeScript/JavaScript. It provides decorators for classes and properties. Perfect worked with MobX.

Class decorator @validatable injects method validate(propName?: keyof this) and property with validation errors (default is validationErrors) which will available after validate method will be called.

React + MobX + Valtors Example

// Store for react component

import { action, observable } from 'mobx';
import { email, required, validatable } from 'valtors';

// @validatable without arguments injects `validationErrors` property for validation errors
@validatable('errors')
export default class AuthCredentialsStore {
  @observable
  @required()
  @email()
  username;

  @observable
  @required('Password is required')
  password;

  @action
  submit() {
    // Validate all fields.
    if (!this.validate()) return;
    // If store is valid submit.
  }
}

// React component

import React, { useCallback } from 'react';
import { action } from 'mobx';
import { observer } from 'mobx-react-lite';
import styles from './SignIn.css';

function SignIn({ store }) {
  const onSubmit = useCallback(action((e) => {
    e.preventDefault();
    store.submit();
  }), [store]);

  const onChange = useCallback(action(({ target: { name, value } }) => {
    store[name] = value;
    store.validate(name); // Validate only one field.
  }), [store]);

  const { errors } = store;

  return (
    <form onSubmit={onSubmit} noValidate>
      <input value={store.username} onChange={onChange} name="username" type="email" placeholder="email">
      <span className={errors.username.error ? styles['error'] : styles['hide']}>{errors.username.error}</span>

      <input value={store.password} onChange={onChange} name="password" type="password" placeholder="password">
      <span className={errors.password.error ? styles['error'] : styles['hide']}>{errors.password.error}</span>

      <button className={styles['btn-login']} type="submit">Login</button>
    </form>
  );
}

export default observer(SignIn);

License

MIT

Package Sidebar

Install

npm i valtors

Weekly Downloads

46

Version

2.6.1

License

MIT

Unpacked Size

110 kB

Total Files

22

Last publish

Collaborators

  • vzh