Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leny/kompare
Compare an object with a simple schema.
https://github.com/leny/kompare
Last synced: 2 days ago
JSON representation
Compare an object with a simple schema.
- Host: GitHub
- URL: https://github.com/leny/kompare
- Owner: leny
- License: unlicense
- Created: 2016-01-24T09:31:32.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-01-29T09:46:22.000Z (about 9 years ago)
- Last Synced: 2025-01-29T17:48:00.669Z (23 days ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kompare
[](https://www.npmjs.com/package/kompare) [](https://travis-ci.org/leny/kompare)  
> Compare an object with a simple schema.
* * *
## Getting Started
### From **browsers** and **node**.
Install the module with: `npm install kompare`.
> **NOTE:** kompare use the ES6 module definition. For now, it uses Babel to transpile to ES5.
> So, if you use ES5, you should require kompare with `var kompare = require( "kompare" ).default;`, and, if you use ES6, you should import kompare with `import kompare from "kompare";`.## Documentation
**kompare** returns a `boolean` indicating if the first object conforms to the simple schema given in the second object. Extra properties of the first objects are ignored.
> **NOTE:** kompare is a young lib used for testing in some of my pro-work. It could (and should) change and evolve in the future. For now, its very simple.
### Signature
`is_conform = kompare( source_object, schema_object[, strict_mode ] );`
#### Example
```javascript
var source = {
"name": "Pierre-Antoine Delnatte",
"age": 30,
"address": "Liège, Belgium",
"skills": [ "js", "css", "html" ],
"experience": {
"flatland": {
"from": 2011
}
}
};var schema = {
"name": "string",
"age": "number",
"address": "string",
"skills": "array",
"experience": "object"
};var conformity = kompare( source, schema );
```
See [tests](./test/kompare_test.js) for other examples.
### Schema object
The **schema object** is a simple javascript object indicating the type (as `string`) of each property in **source object**. The value of a schema property can also be a `boolean`, indicating if the property **must** be present (or not) in the source object, regardless of his type.
### Strict mode
When `strict_mode` is set to `true`, the `source_object` must be conform to the `schema_object` and cannot have extra properties.
## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
## Release History
* **0.1.0**: Initial release (*24/01/16*)
## License
(Un)licensed under the UNLICENSE, 2016.