define a schema, validate the world.
meteor add zhaoyao91:schema-validation
Based on Validations, this package helps you build validation(s) by schema(s) you defined, to test, validate, and check variables.
You can use different Schema Language to define your schema, and different Reporter to format the reports generated by validations.
What's more, you can write your own schema language and reporter and use them.
The goals are:
- separate the schema and the process of validation
- open to define schema languages
- the schema is easy to write and read (depends on languages)
- the schema is as simple as a plain js object (depends on languages)
- the schema reflects the structure of the target object clearly (depends on languages)
- open to define reporters
- the report is easy to read (depends on reporters)
- validations can test(return boolean), validate(return report) or check(throw error) anything.
- open to set default actions
- open to add validations
A schema validation is an object which translates a schema into a validation, and holds it to help you test, validate and check variables.
var sv = new SchemaValidation(schema, [options])
- options.language - the schema language used to translate the schema.
- options.reporter - the reporter used to format the validation report.
- options.actions
- options.actions.quickReturn - if false, the validation will check all branches even if it can decide the result.
- options.actions.reportPass - if true, the report will include reports of sub validations that contribute to positive result.
- options.actions.reportFail - if true, the report will include reports of sub validations that contribute to negative result.
An instance of schema validation has the following methods:
- test(target) => boolean
- validate(target) => report
- check(target) throws Match.Error
If you have a batch of schemas, or if you want to build cycle-reference validations, you can use a batch of schemas to new a batch of validations. see doc.
var svs = SchemaValidation.batchNew(schemas, options);
In this way, the schemas is an object with the key to be the name of the schema and the value to be the schema in which you can refer to other schema as its sub schemas (to build sub validations). How to define schema reference is a thing of a specific schema language.
A schema language is any object that can translate some kind of schema into a simple schema. It must have the following methods:
- parse(some kind of schema) => simple schema
- updateValidationList(list) - used to inform the language that it should update validation list in case some new validation is added.
A reporter is any object that can format the report of a validation into something else. It must have the following methods:
- makeReport(report) => formatted report
The SchemaValidation
is shipped with a set of default options:
- options.language = basicLanguage - a basic language that just receives and returns the simple schema itself.
- options.reporter = basicReporter - a basic reporter that just receives and returns the original report itself.
- options.actions = {quickReturn: true, reportPass: false, reportFail: true}
You can override any item of default options by SchemaValidation.setDefaultOptions(options)
.
You can add validations by any of the following methods. for more, see doc.
- SchemaValidation.addValidation(validationClass)
- SchemaValidation.addValidationByPrototype(prototype)
- SchemaValidation.addValidationByTestFunc(validationName, testFunc)
You can enable/disable string validations by SchemaValidation.stringValidations.enable(nameFunc)/disable()
. for more, see doc.
In the project with this package, run meteor test-packages zhaoyao91:schema-validation
.
- update the logic of extending options
Copyright (c) 2015 Zhao Yao <zhaoyao91@163.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.