Kya - Javascript Object Validation
Kya is written to make object validation easier. It support JS native type, required field and easy customized validate function, all in single object as descriptor. The validation is asynchronous.
Install
npm i kya --save
Support type
- String
- Number
- Date
- Boolean
- Array
- Array validation
- Nested object validation
Examples
const usernameValidator = {
required: true,
type: String,
}
const schema = kya(
{
username: usernameValidator,
telephone: Number,
},
{
username: {
required: 'username is required',
type: 'Type error!'
},
telephone: {
type: 'Type error!'
},
}
)
schema
.validate({ username: 'kya', age: 19 })
.then(result => {
console.log('result', result)
})
API
kya
function kya(schema?: Schema, messages?: Messages): {
validate: (target?: Object) => Promise<Object>;
validateOn: (target?: Object, ...onfields: string[]) => Promise<Object>;
};
interface Schema {
[field: string]: FieldSchema;
}
type SupportType = String | Number | Boolean | Array<any> | Date | Object;
type Type = { type: string; } | SupportType;
type FieldSchema =
(Type & {required?: boolean; }) &
{
[customrule: string]: any
};
export interface Messages {
[field: string]: Object;
}
There are two arguments, schema and message, in kya.
Schema is an object that specify fields you want to check, and in each field you wanna specify field schema, where your validation rule goes here.
Message is the error message it gonna show whenever your field validation is not valid.
0.2.0 (17/Jan/2019)
Added
Support Javascript native types including:
- String
- Number
- Date
- Boolean
- Array
- Array<type> validation
- Nested object validation
Changed
If there's only type checking in schema, schema can omit "type" key.
Before:
const schema = kya({
nested: {
type: {
position: {type: [Number]},
team: {
apple: {type: String},
banana: {type: String},
},
}
},
});
After:
const schema = kya({
nested: {
position: [Number],
team: {
apple: String,
banana: String,
},
}
})