An open API service indexing awesome lists of open source software.

https://github.com/aeberdinelli/schemy-child-settings

Allow using settings in child schemas without creating a Schemy instance separately.
https://github.com/aeberdinelli/schemy-child-settings

json library nodejs schemy validation

Last synced: 2 months ago
JSON representation

Allow using settings in child schemas without creating a Schemy instance separately.

Awesome Lists containing this project

README

          

## About
This plugin for Schemy will allow you to add settings to child schemas. Currently, the only way to do that is creating a Schemy instance separately before using it as a property type in the parent schema.

## Install
`npm install --save schemy-child-settings`

## Usage
Let's go directly into an example:

```javascript
const Schemy = require('schemy');
const ChildSettingsPlugin = require('schemy-child-settings');

// Load the plugin into Schemy
Schemy.extend(ChildSettingsPlugin);

// Now you can do something like this
const schema = new Schemy({
name: {
schema: {
firstname: String,
lastname: String
},
settings: {
strict: true
}
}
}, { strict: false });

schema.validate({
name: {
firstname: 'Firstname',
lastname: 'Lastname',
middlename: 'Middlename'
},
age: 21
}); // => false

schema.getValidationErrors(); // => [ 'Property name.middlename not valid in schema' ]
```

What happens in the example above, is that we define the parent schema as not strict. That way we can add the `age` property even if is not defined. However, we set the `name` schema as `{ strict: true }` so when we try to add the `middlename` property we get the error that is not valid.

## Compatibility
Requires Schemy version `>= 3.1.0`