2.1.1 • Published 4 years ago

vdator v2.1.1

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

vDator

vDator is a VERY SIMPLE javascript and typescript validation library. At the beginning I want to say that if you find any bugs (I test everything but you know) or have any suggestions, please tell me about it. As I said, this library is really simple. I wrote it in typescript so it works well with it. If you are looking for something perfect, 100% safe and certainly without bugs, this library is not for you. I created vDator to do really simple validation on my backend or front without a lot of code.

docs

installation

In order to start using vDator you have to add it into your project. At the moment you can do that using npm.

npm install vDator --save

Basicly that's all! All you have to do now is require or import this package.

vDator has two classes now. One is made for single value validation but other is made for object validation. If you want to use single value validation import vSchema from vDator or vObjectSchema in order to use object validation.

const { vObjectSchema, vSchema } = require('./vDator');

or

import { vObjectSchema, vSchema } from './vDator';

vDator returns object of classes so you could do something like that too.

const vDator = require('./vDator');

vDator.vSchema(/*...*/);

creating schemes

Next step is to create a schema. In order to create the schema you have to pass schema into vSchema or vObjectSchema. When creating vObjectSchema we have to pass "cut" also which is boolean which tells us if we want data to be identical as our schema.

single value schema

const schema = new vSchema({
	isString: true,
	minLength: 10,
	maxLength: 50,
	contains: ['x', 'z'],
});

As you can see we have to create instance of vSchema and pass our Schema as object inside.

object schema

const schema = new vObjectSchema(
	{
		name: {
			isString: true,
			minLength: 2,
		},
		surname: {
			isString: true,
			minLength: 2,
		},
		age: {
			isNumber: true,
			min: 1,
		},
	},
	false
);

In that case we are making instance of vObjectSchema and passing object inside. Have on mind that if you will pass something other inside than object both in vSchema and vObjectSchema it will throw an error. For example

new vSchema('omg so cool');

throws an error.

Of course we are able to store our schema in other variables.

single value schema

const schema = {
	isString: true,
	minLength: 10,
	maxLength: 50,
	contains: ['x', 'z'],
};

/*...*/

new vSchema(schema);

object schema

const schema = {
	name: {
		isString: true,
		minLength: 2,
	},
	surname: {
		isString: true,
		minLength: 2,
	},
	age: {
		isNumber: true,
		min: 1,
	},
};

/*...*/

new vObjectSchema(schema, true);

if you want you can even store schemes for single fields outside and use it in object schemes.

const nameSchema = {
	isString: true,
	minLength: 2,
};

const schema = {
	name: nameSchema,
	surname: {
		isString: true,
		minLength: 2,
	},
	age: {
		isNumber: true,
		min: 1,
	},
};

/*...*/

new vObjectSchema(schema, false);

What is cut?

If we want be 100% sure that our object has got only fields we provided we have to pass true otherwise false.

const schema = {
	name: {
		isString: true,
		minLength: 2,
	},
};
//With false
const schema = new vObjectSchema(schema, false);

schema.compare({
	name: 'Tom',
	age: 38,
});

//we will get {value: {name: 'Tom', age: 38}, err: null}

//With true
const schema = new vObjectSchema(schema, false);

schema.compare({
	name: 'Tom',
	age: 38,
});

//we will get {value: {name: 'Tom'}, err: null}

We won't get fields which are not provided in our schema.

validation

The last step is to compare the data with our scheme.

In order to do that we have to call a compare function (async) or compareSync (sync) on our schema. The compare function returns promise which can be rejected so we have to handle an error.

single value schema

/*...*/
const nameSchema = new vSchema({
	isString: true,
	minLength: 2,
});

nameSchema
	.compare('Bob')
	.then((res) => console.log(res)) /*logs {value: 'Bob', err: null}*/
	.catch((err) => console.log(err)); /*logs {value: null, err: error goes here} if error occured*/

//or

try {
	const res = nameSchema.compare('Bob');

	console.log(res); /*logs {value: 'Bob', err: null}*/
} catch (err) {
	console.log(err); /*logs {value: null, err: error goes here} if error occured*/
}

//sync
const data = nameSchema.compareSync('Bob');
console.log(data); //You have to check if data.err is null

object schema

const userSchema = new vSchema({
	name: {
		isString: true,
		minLength: 2,
		maxLength: 50,
	},
	age: {
		isNumber: true,
		min: 1,
	},
});

userSchema
	.compare(
		{
			name: 'Bob',
			age: 18,
		},
		false
	)
	.then((res) => console.log(res)) /*logs {value: {name: 'Bob', age: 18}, err: null}*/
	.catch((err) => console.log(err)); /*logs {value: null, err: error goes here} if error occured*/

//or

try {
	const res = userSchema.compare(
		{
			name: 'Bob',
			age: 18,
		},
		false
	);

	console.log(res); /*logs {value: {name: 'Bob', age: 18}, err: null}*/
} catch (err) {
	console.log(err); /*logs {value: null, err: error goes here} if error occured*/
}

///sync
const data = nameSchema.compareSync(
	{
		name: 'Bobby',
		age: 34,
	},
	false
);
console.log(data); //You have to check if data.err is null

As you can see we are getting object back with value and err fields. if err field is null validation went well otherwise no. We should use data from value field after vaidation. We can acces it with code like that.

const data = {
	name: 'Bob',
	age: 18,
};

const userSchema = new vObjectSchema({
	name: {
		isString: true,
		minLength: 2,
		maxLength: 50,
	},
	age: {
		isNumber: true,
		min: 1,
	},
});

userSchema
	.compare(data, false)
	.then((res) => {
		const user = res.value;

		//Use user variable for example to save data to a database. Don't use data varialbe in order to be sure that data is valid
	})
	.catch((err) => {
		console.log(err.err); //for debugging purpose

		//send information to client that something goes wrong
	});

Objects

When creating object schemes you can enter required method and set it to false (true is by defaut so you do not have to even add this method). After adding it this field will be extra (not required).

const schema = new vObjectSchema({
	name: {
		isString: true,
		minLength: 2,
	},
	secondName: {
		isString: true,
		minLength: 2,
		required: false,
	},
});

schema
	.compare({ name: 'Bob' }, false) // This works because secondName is extra field
	.then((res) => console.log(res))
	.catch((err) => console.log(err));

That's all! In rest of this README you will found all available methods you can use in writing your schemes and how they work.

Available methods

First, I want to clear up something that should eliminate any confusion.

new vSchema({
	isString: true,
}).compare(data, false);

In that case isString is method and true is value so isString accepts boolean values and checks if data is string. That's all :D

Strings

methoddesc
isStringAccepts boolean values. Checks if data is string
regExAccepts values instanceof RegExp. Checks if data matches provided regular expression
isEmailAccepts boolean values. Checks if data fits email RegExp /^[-!#-'*+\/-9=?^-~]+(?:\.[-!#-'*+\/-9=?^-~]+)*@[-!#-'*+\/-9=?^-~]+(?:\.[-!#-'*+\/-9=?^-~]+)+$/i
isMongoDBidAccepts boolean values. Checks if data fits MongoDB ObjectID RegExp /^[0-9a-fA-F]{24}$/
isBase64Accepts boolean values. Checks if data fits base64 RegExp /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/
isStringNumberAccepts boolean values. Checks if data is number "5" ==> true || "abc" ==> false
containsAccepts object instanceof Array filled with strings. Checks if data contains values in array value: ["a"] || "bc" ==> false || "abc" ==> true
withoutAccepts object instanceof Array filled with strings. Checks if data doesn't includes provided strings value: ["a"] || "bc" ==> true || "abc" ==> false
isUrlAccepts boolean values. Checks if data is valid url "https://github.com/" ==> true || "www.github.com" ==> false
isAlphaNumericAccepts boolean values. Checks if data is alphanumeric "aaddvv_d" ==> true || ".!@#" ==> false

Numbers

methoddesc
isNumberAccepts boolean values. Checks if data is number
minAccepts number values. Checks if the data is greater than or equal to the value value: 1 || 5 ==> true || -2 ==> false
maxAccepts number values. Checks if the data is smaller than or equal to the value value: 5 || 5 ==> true || 6 ==> false
isEvenAccepts boolean values. Checks if the provided number is Even value: 4 || true

Universal

methoddesc
equalsAccepts any value. Checks if data is the same as value (===) value: 5 || 5 ==> true || "5" ==> false
isSimilarAccepts any value. Checks if data is similar to the value (==) value : 5 || 5 ==> true || "5" ==> true
maxLengthAccepts number values. Checks if data is shorter or equal to provided number value: 5 || "abcdef" ==> false || "abcde" ==> true
minLengthAccepts number values. Checks if data is longer or equal to provided number value: 5 || "abcd" ==> false || "abcde" ==> true

I will upgrade this library so have on mind that there would be little changes in the future BUT it won't change syntax.

2.1.1

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago