Skip to content

Releases: icebob/fastest-validator

v0.6.11

04 Nov 16:52
Compare
Choose a tag to compare

Changes

v0.6.10

25 Jun 20:07
Compare
Choose a tag to compare

Changes

  • fix #27 - multiple optional validators

v0.6.9

07 Jun 11:12
Compare
Choose a tag to compare

Changes

  • fix #25 - multiple optional validators
  • Add new enum rule
        { type: "enum", values: ["male", "female"] }

v0.6.7

29 May 08:24
Compare
Choose a tag to compare

Changes

  • supports multiple object validators #22 by @mauricedoepke
    const schema = {
        list: [
            { 
                type: "object",
                props: {
                    name: {type: "string"},
                    age: {type: "number"},
                } 
            },
            { 
                type: "object",
                props: {
                    country: {type: "string"},
                    code: {type: "string"},
                } 
            }
        ]
    };

v0.6.6

04 Apr 13:13
Compare
Choose a tag to compare

0.6.6 (2018-04-04)

Access to the original object in custom validator #5

const schema = {
    email: { 
        type: "custom", 
        check(value, schema, stack, obj) {
            return obj.username || obj.email ? null : this.makeError(...);
        }
    }
};

v0.6.3

07 Sep 20:20
Compare
Choose a tag to compare

Browser support

From now the library is working in browsers as well. Thanks for incubus8!

Example in vanilla JS:

<script src="https://unpkg.com/fastest-validator"></script>
var v = new FastestValidator();

const schema = {
	multi: [
		{ type: "string", min: 3, max: 255 },
		{ type: "boolean" }
	]
};

console.log(v.validate({ multi: "John" }, schema));
// Returns: true

No dependencies

We removed all dependencies.

v0.5.0

03 Jul 12:11
Compare
Choose a tag to compare

Multiple validators

There is possible to define more validators for a field. In this case if one of all validators is success, the field will be valid.

let schema = {
    cache: [
        { type: "string" },
        { type: "boolean" }
    ]
}

v.validate({ cache: true }, schema); // Valid
v.validate({ cache: "redis://" }, schema); // Valid
v.validate({ cache: 150 }, schema); // Fail

Custom validator

You can also create your custom validator.

let v = new Validator({
    messages: {
        // Register our new error message text
        evenNumber: "The '{field}' field must be an even number! Actual: {actual}"
    }
});

// Register a custom 'even' validator
v.add("even", value => {
    if (value % 2 != 0)
        return v.makeError("evenNumber", null, value);

    return true;
});

const schema = {
    name: { type: "string", min: 3, max: 255 },
    age: { type: "even" }
};

console.log(v.validate({ name: "John", age: 20 }, schema));
// Returns: true

console.log(v.validate({ name: "John", age: 19 }, schema));
/* Returns an array with errors:
    [{
        type: 'evenNumber',
        expected: null,
        actual: 19,
        field: 'age',
        message: 'The \'age\' field must be an even number! Actual: 19'
    }]
*/

Or you can use the custom type with inline checker function:

let v = new Validator({
	messages: {
		// Register our new error message text
		weightMin: "The weight must be larger than {expected}! Actual: {actual}"
	}
});

const schema = {
	name: { type: "string", min: 3, max: 255 },
	weight: { 
		type: "custom", 
		minWeight: 10, 
		check(value, schema) {
			return (value < schema.minWeight)
				? this.makeError("weightMin", schema.minWeight, value)
				: true;
		}
	}
};

console.log(v.validate({ name: "John", weight: 50 }, schema));
// Returns: true

console.log(v.validate({ name: "John", weight: 8 }, schema));
/* Returns an array with errors:
	[{ 
		type: 'weightMin',                                       
		expected: 10,                                            
		actual: 8,                                               
		field: 'weight',                                         
		message: 'The weight must be larger than 10! Actual: 8' 
	}]
*/