1.0.5 • Published 3 years ago

ottervalidation v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

OtterValidation

Simple, lightweight validation with full typescript support

Installation

npm install ottervalidation

Full example

import { OV, OVValidation } from 'ottervalidation';

interface Form {
  username: string,
  password: string,
  mailAddress: string,
}

const form: Form = {
  username: 'Some username',
  password: 'SomeP4ssword!',
  mailAddress: 'some@email.com',
};

const validation: OVValidation<Form> = {
  username: {
    required: true,
    type: 'string',
    minLength: 4,
    maxLength: 32,
  },
  password: {
    required: true,
    type: 'string',
    minLength: 8,
    maxLength: 128,
    minUpperCase: 1,
    minLowerCase: 1,
    minNumeric: 1,
    minSymbol: 1,
  },
  mailAddress: {
    required: true,
    type: 'string',
    email: true,
  },
};

const ov = new OV(form, validation);
const ovResult = ov.validate();
/*
  ovResult: {
    object: {
      username: {},
      password: {},
      mailAddress: {},
    }
  }
*/

Example with errors

import { OV, OVValidation } from 'ottervalidation';

interface Form {
  username: string,
  password: string,
  mailAddress: string,
}

const form = {
  password: 'invalid length',
  mailAddress: 'invalid email',
} as Form;

const validation: OVValidation<Form> = {
  username: {
    required: true,
  },
  password: {
    minLength: 32,
  },
  mailAddress: {
    email: true,
  },
};

const ov = new OV(form, validation);
const ovResult = ov.validate();
/*
  ovResult: {
    errors: ['username.required', 'password.minlength', 'mailAddress.email'],
    object: {
      username: { errors: ['username.required'] },
      password: { errors: ['password.minlength'] },
      mailAddress: { errors: ['mailAddress.email'] },
    }
  }
*/

Validation types

KeyArgumentDescriptionWorks with
requiredbooleanchecks whether the key exists in the objectstring, number, boolean
typestringstring[]Checks whether the value is of the given typestring, number, boolean
minLengthnumberChecks whether the value has the minimum lengthstring
maxLengthnumberChecks whether the value has the maximum lengthstring
exactLengthnumberChecks whether the value has the exact lengthstring
emailbooleanChecks whether the value is an e-mail addressstring
minUpperCasenumberChecks whether the value has the minimum number of uppercase lettersstring
minLowerCasenumberChecks whether the value has the minimum number of lowercase lettersstring
minNumericnumberChecks whether the value has the minimum number of numericsstring
minSymbolnumberChecks whether the value has the minimum number of symbol charactersstring
regexRegExpChecks whether the value matches against the regexstring

Configuration Example

...

const config: OVConfiguration = {
  errorMessage: {
    prefix: 'validation',
    addKeyPrefix: true,
    override: {
      'validation.username.required': 'Username is required',
    }
  }
}

const ov = new OV(form, validation, config);
const ovResult = ov.validate();
/*
  ovResult: {
    errors: ['Username is required'],
    object: {
      username: { errors: ['Username is required'] },
    }
  }
*/

Configuration

KeyArgumentDescription
errorMessage.prefixstringAdds a prefix to the error message
errorMessage.addKeyPrefixbooleanAdd the key prefix to error message (default true)
errorMessage.overrideRecord<string, string>Overrides the error message

Contributing

# install dependencies
npm install

# run all tests
npm test

# run linter
npm run lint

License

MIT