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.
- Host: GitHub
- URL: https://github.com/aeberdinelli/schemy-child-settings
- Owner: aeberdinelli
- Created: 2021-08-18T13:42:26.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-19T05:35:16.000Z (almost 5 years ago)
- Last Synced: 2025-01-12T11:30:54.040Z (over 1 year ago)
- Topics: json, library, nodejs, schemy, validation
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/schemy-child-settings
- Size: 2.93 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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`