Skip to content

aeberdinelli/schemy-child-settings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

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