thesmo-validatorsdeprecated

Validators collection

Usage no npm install needed!

<script type="module">
  import thesmoValidators from 'https://cdn.skypack.dev/thesmo-validators';
</script>

README

npm GitHub GitHub last commit npm

Summary

This package contains collection of useful methods for values validating.

API

API methods clarification

IsArray

Signature

/* arguments object */
({ 

})
/* returns */
(target: *) => boolean

Usage example

const { IsArray } = require("thesmo-validators");

const array = [0,1,2];
const notAnArray = {hello: 1, world: 2};

IsArray({})(array) //true
IsArray({})(notAnArray) //false

IsBoolean

Signature

/* arguments object */
({ 
    allowNumericRepresentation?: boolean
    allowStringRepresentation?: boolean
})
/* returns */
(target: *) => boolean

Usage example

const { IsBoolean } = require("thesmo-validators");

const bool = true;
const anotherBool = false;
const digit = 42;
const falseAsString = "false";
const falseAsNumber = 0;

IsBoolean({})(bool) //true
IsBoolean({})(anotherBool) //true
IsBoolean({})(digit) //false
IsBoolean({ allowStringRepresentation: false })(falseAsString) //false
IsBoolean({ allowStringRepresentation: true })(falseAsString) //true
IsBoolean({ allowNumericRepresentation: false })(falseAsNumber) //false
IsBoolean({ allowNumericRepresentation: true })(falseAsNumber) //true

IsNumber

Signature

/* arguments object */
({ 
    allowStringRepresentation?: boolean
})
/* returns */
(target: *) => boolean

Usage example

const { IsNumber } = require("thesmo-validators");
const { Create } = require("thesmo-redux-store");

const testStore = Create({
    defaultState: {
        a: 42,
        b: "42",
        c: "_42_",
        d: 52.1,
        e: "52,1",
        f: "52.1"
    }
})

const isNumber = IsNumber({ 
    allowStringRepresentation: false 
});

const isNumberForced = IsNumber({
    allowStringRepresentation: true
});

testStore.From("a", isNumber) //true;
testStore.From("b", isNumberForced) //true;
testStore.From("c", isNumberForced) //false;
testStore.From("d", isNumber) //true;
testStore.From("e", isNumberForced) //false;
testStore.From("f", isNumberForced) //true;
testStore.From("f", isNumber) //false;

IsObject

Signature

/* arguments object */
({ 

})
/* returns */
(target: *) => boolean

Usage example

const { IsObject } = require("thesmo-validators");

const a = { };
const b = { field_1: 1, field_2: 55 };
const c = null;
const d = [1, 2, 3];
const e = 42;

IsObject({})(a); //true
IsObject({})(b); //true
IsObject({})(c); //false
IsObject({})(d); //false
IsObject({})(e); //false

IsPrimitive

Signature

/* arguments object */
({ 

})
/* returns */
(target: *) => boolean

Usage example

const { IsPrimitive } = require("thesmo-validators");

IsPrimitive({})(1); //true
IsPrimitive({})("1"); //true
IsPrimitive({})(true); //true
IsPrimitive({})(null); //false
IsPrimitive({})({a: 24}); //false