scem v3.0.1
Scem
Scem is a small js package to validate object by using schema
How to create schema
Here how to create a schema
const schema = new Schema({ foo: { type: "string", required: true } });
How to validate an object
Now that your schema is created it would be nice to use it to validate an object. Here is how you can do it
const isValid = schema.validate({ foo: "hello world" });
You can also activate the errors options to return get back an error array on each property (by default this options is set to false)
const { valid, errors } = schema.validate({ foo: "hello world" }, { errors: true }) // will return {valid: true, errors: [] }
If this option is activated and there is no errors the errors property will be an empty array but if there is an error on a property for example we expect a string, but we got a number it will send back an array like this :
const errors = [{
property: "foo",
error: "Invalid type, type must be string"
}]
Conditions
type
With this condition you can specify the type of the value
const schema = new Schema({ foo: { type: "string" } })
the possible values are :
- string
- number
- boolean
- array
required
This condition can be used to specify if the property is needed or not if you omit it, it will be equal to false
const schema = new Schema({ foo: { required: true } })
the possible values are :
- true
- false
min
This condition can be used to specify a min value for a number
const schema = new Schema({ foo: { min: 0 } })
When validating a number this condition will be valid if the number is equal or superior to min
const valid = schema.validate({ foo: 0 }) // valid = true
max
This condition can be used to specify a max value for a number
const schema = new Schema({ foo: { max: 1 } })
When validating a number this condition will be valid if the number is equal or less to max
const valid = schema.validate({ foo: 1 }) // valid = true
minLength
this condition can be used to specify a minLength for a string or an array
const schema = new Schema({ foo: { minLength: 1 } })
When validating this condition will be valid if the length is equal or superior to minLength
const valid = schema.validate({ foo: "t" }) // valid = true
maxLength
this condition can be used to specify a maxLength for a string or an array
const schema = new Schema({ foo: { maxLength: 1 } })
When validating this condition will be valid if the length is equal or less to maxLength
const valid = schema.validate({ foo: "t" }) // valid = true
regex
this condition can be used to check if a string match a regex. If the string doesn't match it will return false
const schema = new Schema({ foo: { regex: /\d/gi } });
expected
this condition can be used to specify an expected value for a property
const schema = new Schema({ foo: { expected: 1 } });
/!\ this condition works only for primitive types : string, number, boolean
validator
This condition allow you to define your own custom validator. To create a custom validator you have to create a function that take one parameter and throw an error on conditions
/!\ One important thing to note is that the validator is always the last condition to be checked
const isEven = (value: any): boolean => {
if (value % 2 !== 0) {
throw new Error("Value is not even");
}
}
Once your validator is created you can use it like that
const schema = new Schema({ foo: { validator: isEven } });