0.0.7 • Published 3 years ago
kgbschema v0.0.7
kgbschema
Installation
import { KGBSchema ,T } from 'kgbschema';
or
const { KGBSchema ,T } = require('kgbschema');
Usage
1. Create type function to validate property value
const string = function(value){
return typeof value === 'string' || `value type "${value}" is not a string`
}
const products = function(value){
return value === 'shampoo' || value === 'cigarettes' || 'the value of product should be shampoo or cigarettes'
}
2. Instantiate a new schema class and register type
const kgb = new KGBSchema({
client : {
username : T.type('string')
},
products : [{
name : T.type('products')
}]
}).registerTypes(string,products)
3. Validate a new object against schema
let object = {
client : {
username : 'Mike Dodd'
},
products : []
}
kgb.push(object).then((r) => {
console.log(r);
}).catch((e) => {
console.log(e);
})
4. Validate the existing object against schema
type Error = {
path : string
message : string
}
let query = {
'client.username' : 'Denis Potrovsky',
}
kgb.update(query).then(() => {
// successfuly passed the validation
}).catch((errors:Error[]) => {
// handle the errors
console.error(e);
})