Javascript error checking library
Javascript input and schema checking library.
``bash`
npm install microcheck.js
The module is packaged for all module style.
The main one in for ES6 import.
For general import.
`javascript`
import microcheck.js from 'micro-check.js';
`javascript`
const microcheck = require('micro-check.js');
This for browser environment generally.
It will create a microcheck global variable.
`javascript`
javascript
// this is the schema to check
const schema = {
name: String, // define types you want to validate and keys name
data: Array,
functions: Object,
};const schemaGood = {
name: 'test',
data: [],
functions: {
test: function() {
return true;
},
},
};
const schemaBad = {
name: 'test',
data: {},
functions: {},
};
const schemaBadKey = {
name: 'test',
data: {},
};
check.validate(schemaGood, schema)
// null
check.validate(schemaBad, schema)
// Error("data should be of type Array")
check.validate(schemaBadKey, schema)
// Error("missing keys: functions")
`$3
`javascript
const schema = {
name: String,
'?data': Array,
'?functions': Object,
};const schemaGood = {
name: 10,
data: [],
};
const schemaBad = {
name: 'test',
data: {},
};
check.validate(schemaGood, schema)
// null
check.validate(schemaBad, schema)
// Error("data should be of type Array")
`$3
`javascriptconst numberSupSchema = {
age: '>10',
};
const numberMinSchema = {
age: '<10',
};
const numberRangeSchema = {
age: '10<>100',
};
const numberFloatSupSchema = {
age: '>10.15',
};
const numberFloatMinSchema = {
age: '<10.15',
};
const numberFloatRangeSchema = {
age: '10.4<>10.7',
};
check.validate({ age: 11 }, numberSupSchema);
// null
check.validate({ age: 11 }, numberSupSchema);
// null
check.validate({ age: -11 }, numberSupSchema);
// Error("must respect age > 10")
check.validate({ age: 10 }, numberSupSchema);
// Error("must respect age > 10")
check.validate({ age: 9 }, numberMinSchema);
// null
check.validate({ age: -9 }, numberMinSchema);
// null
check.validate({ age: 10 }, numberMinSchema);
// Error("must respect <10");
check.validate({ age: 50 }, numberRangeSchema);
// null
check.validate({ age: 11 }, numberRangeSchema);
// null
check.validate({ age: 10 }, numberRangeSchema);
// Error("must respect 10<>100")
check.validate({ age: 100 }, numberRangeSchema);
// Error("must respect 10<>100")
check.validate({ age: 10.16 }, numberFloatSupSchema);
// null
check.validate({ age: 10.16 }, numberFloatSupSchema);
// null
check.validate({ age: -11 }, numberFloatSupSchema);
// Error("must respect >10.15");
check.validate({ age: 10.14 }, numberFloatSupSchema);
// Error("must respect >10.15");
check.validate({ age: 10.14 }, numberFloatMinSchema);
// null
check.validate({ age: -9 }, numberFloatMinSchema);
// null
check.validate({ age: 10.15 }, numberFloatMinSchema);
// Error("must respect <10.15");
check.validate({ age: 10.5 }, numberFloatRangeSchema);
// null
check.validate({ age: 10.6 }, numberFloatRangeSchema);
// null
check.validate({ age: 10.2 }, numberFloatRangeSchema);
// Error("must respect 10.4<>10.7");
check.validate({ age: 10.7 }, numberFloatRangeSchema);
// Error("must respect 10.4<>10.7");
`$3
`javascript
const schema = {
name: new RegExp("^[a-zA-Z0-9\-_]{1,}$"),
"?data": Object,
};check.validate({ name: "ok" }, schema);
// null
check.validate({ name: "not ok" }, constructorSchema);
// Error("must respect {
name: new RegExp("^[a-zA-Z0-9\-_]{1,}$"),
"?data": Object,
}");
check.validate({ name: "test" }, { name: 'test' });
// null
check.validate({ name: "notok" }, { name: 'test' });
// TypeError;
`$3
`javascriptconst arrayGood = ['test', 10, { name: 'inside_test' }, [], [10]];
// order of data is not important
// [[], [10], 'test', 10, { name: 'inside_test' }],
// ['test', [10], { name: 'inside_test' }, 10, []],
// [[10], 'test', { name: 'inside_test' }, 10, []]
const arrayBad = ['test'];
check.validate({ data: arrayGood }, arraySchema);
// null
check.validate({ data: arrayBad }, arraySchema);
// Error(
must respect ${arrayGood})
`$3
`javascript
const testVars = ['test', 10];
const testInput = { name: 'test' };
const schema = {
name: input => testVars.includes(input),
};
check.validate(testInput, schema);
// null
check.validate({ name: 'test' }, schema);
// null
check.validate({ name: 'lol' }, schema)
// Error("ValidationError: "lol" did not pass input => testVars.includes(input): returned false")
``#### License
Copyright 2018 Ciro DE CARO
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.