lib/model/compiler.js
function compileValidator( attributes ) {
	if ( !attributes || typeof attributes !== "object" || Array.isArray( attributes ) ) {
		throw new TypeError( "definition of attributes must be object" );
	}

	const attributeNames = Object.keys( attributes );
	const numAttributes = attributeNames.length;
	const validation = [];

	for ( let ai = 0; ai < numAttributes; ai++ ) {
		const attributeName = attributeNames[ai];
		const attribute = attributes[attributeName];

		const handler = Types.selectByName( attribute.type );

		const { args, body } = extractBody( handler.isValid );
		if ( args.length < 4 ) {
			throw new TypeError( `isValid() of ModelType for handling ${attribute.type} values must accept four arguments` );
		}

		validation[ai] = `
{
	const ${args[0]} = "${attributeName}";
	let ${args[1]} = __props["${attributeName}"];
	const ${args[2]} = __attrs["${attributeName}"];
	const ${args[3]} = __errors;

	{
		${body}
	}
}
		`;
	}

	// eslint-disable-next-line no-new-func
	return new Function( `
const __attrs = this.constructor.schema.attributes;
const __props = this.properties;
const __errors = [];

${validation.join( "" )}

return __errors;
` );
}
function compileValidator( attributes ) {
	if ( !attributes || typeof attributes !== "object" || Array.isArray( attributes ) ) {
		throw new TypeError( "definition of attributes must be object" );
	}

	const attributeNames = Object.keys( attributes );
	const numAttributes = attributeNames.length;
	const validation = [];

	for ( let ai = 0; ai < numAttributes; ai++ ) {
		const attributeName = attributeNames[ai];
		const attribute = attributes[attributeName];

		const handler = Types.selectByName( attribute.type );

		const { args, body } = extractBody( handler.isValid );
		if ( args.length < 4 ) {
			throw new TypeError( `isValid() of ModelType for handling ${attribute.type} values must accept four arguments` );
		}

		validation[ai] = `
{
	const ${args[0]} = "${attributeName}";
	let ${args[1]} = __props["${attributeName}"];
	const ${args[2]} = __attrs["${attributeName}"];
	const ${args[3]} = __errors;

	{
		${body}
	}
}
		`;
	}

	// eslint-disable-next-line no-new-func
	return new Function( `
const __attrs = this.constructor.schema.attributes;
const __props = this.properties;
const __errors = [];

${validation.join( "" )}

return __errors;
` );
}
function compileValidator ( attributes ) { if ( ! attributes || typeof attributes !== "object" || Array . isArray ( attributes ) ) { throw new TypeError ( "definition of attributes must be object" ) ; } const attributeNames = Object . keys ( attributes ) ; const numAttributes = attributeNames . length ; const validation = [ ] ; for ( let ai = 0 ; ai < numAttributes ; ai ++ ) { const attributeName = attributeNames [ ai ] ; const attribute = attributes [ attributeName ] ; const handler = Types . selectByName ( attribute . type ) ; const { args , body } = extractBody ( handler . isValid ) ; if ( args . length < 4 ) { throw new TypeError ( ` ${ attribute . type } ` ) ; } validation [ ai ] = ` ${ args [ 0 ] } ${ attributeName } ${ args [ 1 ] } ${ attributeName } ${ args [ 2 ] } ${ attributeName } ${ args [ 3 ] } ${ body } ` ; } // eslint-disable-next-line no-new-func return new Function ( ` ${ validation . join ( "" ) } ` ) ; }
Creates validator function assessing all defined attributes of a model.

@param {object<string,object>} attributes definition of essential attributes
@returns {function():Error[]} concatenated implementation of all defined attributes' validation handlers
Creates validator function assessing all defined attributes of a model .