1.2.2 • Published 3 years ago

@sovgut/object-validation v1.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago
import validateObject, { property, Scheme } from '@sovgut/object-validation';

const request = {
  body: {
    email: 'example@email.com',
    password: 'superSecretPassw0rd!',
    confirmPassword: 'superSecretPassw0rd!',
  },
};

const validationScheme: Scheme[] = [
  property('email:string'),
  property('password:string').min(8).max(32),
  property('confirmPassword:string').min(8).max(32).compareWith('password'),
  property('age:number').optional(),
];

const { success, reason } = validateObject(request.body, validationScheme);
console.log(success, reason);

API

property(key: "name:primitive"): Scheme

Params

  • key - first part of this string is the property name from the object, second is the type of property from the object

Returns

  • min(length: number) - sets the minimum of length for string source
  • max(length: number) - sets the maximum of length for string source
  • compareWith(key: string) - sets comparison within source and target properties
  • onValidate(callback: (value: any) => string | void) - sets additional custom validation for source property, should return an error string like "something-wrong-happened" or nothing (void)
  • optional() - sets source property as optional and any validations just skipped this property if error raised

validateObject(target: any, scheme: Scheme[]): Response

Params

  • target - the source object that should be validated
  • scheme - validation scheme array

Returns

  • response - the response object that have success and reason properties, reason is undefined by default, this object have text when error raised
1.2.2

3 years ago

1.2.0

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago