Skip to content

ksobolewski-netins/form-schema-validation

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SCHEMA

Build Status Coverage Status npm npm

1. Installation
2. How to use
3. Constructor
4. Methods
5. Types
6. Example of custom validator
7. Schema definition Example
8. Example of schema in schema
9. Schema keys description
10. Custom validation messages
11. Switch of keys validation

Installation

$ npm install form-schema-validation --save

How to use

Schema give you posibility to validate object using schema validation. You can defined schema and use validate method to check object. Validate method allways returns errors object but if You don't have errors object is empty so You can check errors by

import Schema from 'form-schema-validation';

const schema = new Schema({
    companyName: {
        type: String
    }
});

const modelObject = {
    companyName: 'Test Company'
};

const errors = schema.validate(modelObject); // {}
const error = Object.keys(errors).length > 0; // false

Promises support

You can use validators that return Promise. If You return promis in validator then shema.validate(model) will return Promise.

import Schema from 'form-schema-validation';

const customValidator = {
    validator: (value) => {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve(value === 'test');
            }, 1000);
        });
    },
    errorMessage: 'async test error';
}

const schema = new Schema({
    companyName: {
        type: String,
        validators:[customValidator]
    }
});

const modelObject = {
    companyName: 'Test Company'
};

const results = schema.validate(modelObject); // Promise
results.then((errors) => {
    console.log(Object.keys(errors).length > 0); // true
});

Constructor

Name Type Description
schema Object schema will be used when you validate object
errorMessages Object errors messages that will be displayed on error
validateKeys Boolean this flag give you posibility to don't validate object keys not defined in schema

Methods

Name Attributes Description
validate model: Object Validate Object using defined schema
getDefaultValues Get default values for model using defined schema
getField name: String Get field schema
getField Get all fields schemas
oneOfTypes types: Array of types Give posibility to validate one of type (Static method)

Types

Name Description
String Simple String type
Number Simple Number type
Object Simple Object type this type give you posibility to black box
Boolean Simple Boolean type
Date This type check value is instance of Date
new Schema This type check value is instance of Schema and validate value by this schema
Schema.oneOfType([type1, type2, ...]) This type give you posibility check one of types it will return error if value don't match all types

Example of custom validator

This validator will check two fields. You can validate one field on base another field.

const validateIfFieldTitleIsFilled = (minLength, message) => ({
    validator: (value, fieldSchema, formData) => {
        if(formData.title){
            return !!value;
        }
        return true;
    },
    errorMessage: message
});

Example of dynamic error messages

There can be a need for error messages generated based on the validation outcome. In that case a string or array of strings can be returned from the validator function. Error messages returned from validator function have higher priority that the errorMessage property.

const MIN_AGE = 18;
const validateIfOfAge = () => ({
    validator: (value, fieldSchema, formData) => {
        const { age } = formData;
        if (age <= MIN_AGE) {
            return [`Given ${age} is lower than required age of ${MIN_AGE}`];
        }
    }
});

Schema definition Example

If You want create new schema You must put object to constructor with information about object keys names and type of value on key.

import Schema from 'form-schema-validation';

const min = (minLength, message) => ({
    validator: (value) => {
        return value.length > minLength;
    },
    errorMessage: message
});

const schema = new Schema({
    companyName: {
        type: String,
        required: true,
        label: 'Company name'
        validators: [min(2, 'Company name should be longer then 2 chars')]
    },
    createdAt: {
        type: Schema.oneOfTypes([Date, String]),
        defaultValue: new Date(),
        label: 'When start'
    },
    workers: {
        type: Number
        label: 'How many workers we have'
    }
});
Example of schema in schema
import Schema from 'form-schema-validation';

const userSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    surname: {
        type: String,
        required: true
    },
    age: {
        type: Number
    }
});

const groupSchema = new Schema({
    name: {
        type: String,
        required: true,
        label: 'Group name'
    },
    createdAt: {
        type: Date
        defaultValue: new Date(),
        label: 'Created at'
    },
    members: {
        type: [userSchema]
        label: 'Members'
    }
});

Schema keys description

When You defined schema You can use this keys:

Key Allowed values Description
companyName, createdAt, workers, ... any name this key defined object key name
type String, Number, Object, Date, Boolean, Array, instance of Schema, [String] ... this key tell as what type of value we should have on this key in model
required true, false this key tell as that field is required
defaultValue Any You can set default value for model
options Array of (String, Number, Object, Date, ...) If you use schema for forms You can defined options for select field
label Any instance of String If you use schema for forms You can defined label for form field
validators array of Functions You can add custom validators for validate field for example min or max length of value.

Custom validation messages

import Schema from 'form-schema-validation';

const ErrorMessages = {
    notDefinedKey(key) { return `Key '${key}' is not defined in schema`; },
    modelIsUndefined() { return 'Validated model is undefined'; },
    validateRequired(key) { return `Field '${key}' is required`; },
    validateString(key) { return `Field '${key}' is not a String`; },
    validateNumber(key) { return `Field '${key}' is not a Number`; },
    validateObject(key) { return `Field '${key}' is not a Object`; },
    validateArray(key) { return `Field '${key}' is not a Array`; },
    validateBoolean(key) { return `Field '${key}' is not a Boolean`; },
    validateDate(key) { return `Field '${key}' is not a Date`; }
};

const groupSchema = new Schema({
    name: {
        type: String,
        required: true,
        label: 'Group name'
    },
    createdAt: {
        type: Date
        defaultValue: new Date(),
        label: 'Created at'
    },
    members: {
        type: [userSchema]
        label: 'Members'
    }
}, ErrorMessages);

Switch of keys validation

import Schema from 'form-schema-validation';

const schema = new Schema({
    companyName: {
        type: String,
        required: true
    }
}, false, false);

const modelObject = {
    companyName: 'Test Company',
    _id: 'test1234567890',
};

const errors = schema.validate(modelObject);
console.log(Object.keys(errors).length > 0); // false

About

Schema for form validation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%