<commit_before>"use strict";

var _ = require('lodash');
var Schema = require('../index');

module.exports = {
	'Simple Usage: build - equal builds made by different approaches': function (test) {
		var sh1 = new Schema().object(function (required, optional) {
			this.field('first_name');
			required('last_name');
			optional('middle_name');
		});

		var sh2 = new Schema().object(function () {
			this.required('first_name');
			this.field('last_name');
			this.optional('middle_name');
		});

		var sh3 = new Schema().object(function () {
			this.field('first_name');
			this.field('last_name');
			this.field('middle_name').optional();
		});

		var sh4 = new Schema();
			sh4.required('first_name');
			sh4.field('last_name');
			sh4.optional('middle_name');

		test.deepEqual(sh1, sh2);
		test.deepEqual(sh1, sh3);
		test.deepEqual(sh1, sh4);

		test.done();
	},

	'Simple Usage: verify - no specified fields': function (test) {
		var sh1 = new Schema().object(function () {
			this.field('first_name');
			this.required('last_name');
			this.optional('middle_name');
		});

		var value = {};
		sh1.verify(value, function (err, isValid, vError) {
			test.ok(!isValid);
			test.equal(vError.ruleName, 'required');
			test.strictEqual(vError.ruleParams, null);
			test.strictEqual(vError.value, undefined);
			test.deepEqual(vError.path, ['first_name']);
			test.done(err);
		});
	},

	'Simple Usage: verify - optional field isn\'t specified': function (test) {
		var sh1 = new Schema().object(function () {
			this.field('first_name');
			this.required('last_name');
			this.optional('middle_name');
		});

		var value = { first_name: 'hello', last_name: 'world' };
		sh1.verify(value, function (err, isValid, vError) {
			test.ok(isValid);
			test.strictEqual(vError, null);
			test.done(err);
		});
	},

	'Simple Usage: verify - correct if all available fields not empty': function (test) {
		var sh1 = new Schema().object(function () {
			this.field('first_name');
			this.required('last_name');
			this.optional('middle_name');
		});

		var value = { first_name: 'hello', last_name: 'world', middle_name: 'param-pam-pam' };
		sh1.verify(value, function (err, isValid, vError) {
			test.ok(isValid);
			test.strictEqual(vError, null);
			test.done(err);
		});
	},

	'Simple Usage: verify - excess field': function (test) {
		var sh1 = new Schema().object(function () {
			this.field('first_name');
			this.required('last_name');
			this.optional('middle_name');
		});

		var value = {
			first_name: 'hello',
			last_name: 'world',
			middle_name: 'param-pam-pam'
		};
		var _keys = _.keys(value);
		value.excess_field = true;

		sh1.verify(value, function (err, isValid, vError) {
			test.ok(!isValid);
			test.equal(vError.ruleName, 'available_fields');
			test.deepEqual(vError.ruleParams, _keys);
			test.deepEqual(vError.value, value);
			test.deepEqual(vError.path, []);
			test.done(err);
		});
	},

	'Create Schema, Register - equal registering': function (test) {
		var sh1 = new Schema();
		var sh2 = Schema(); // jshint ignore: line
		var sh3 = new Schema('nameForRegister1');
		var sh4 = Schema('nameForRegister2'); // jshint ignore: line
		Schema.register('someSchema1', sh1);
		Schema.register('someSchema2', sh2);

		test.strictEqual(Schema.get('someSchema1'), sh1);
		test.strictEqual(Schema.get('someSchema2'), sh2);
		test.strictEqual(Schema.get('nameForRegister1'), sh3);
		test.strictEqual(Schema.get('nameForRegister2'), sh4);

		test.done();
	},
};<commit_msg>add example test for attachTo<commit_after>"use strict";

var _ = require('lodash');
var Schema = require('../index');

module.exports = {
	'Simple Usage: build - equal builds made by different approaches': function (test) {
		var sh1 = new Schema().object(function (required, optional) {
			this.field('first_name');
			required('last_name');
			optional('middle_name');
		});

		var sh2 = new Schema().object(function () {
			this.required('first_name');
			this.field('last_name');
			this.optional('middle_name');
		});

		var sh3 = new Schema().object(function () {
			this.field('first_name');
			this.field('last_name');
			this.field('middle_name').optional();
		});

		var sh4 = new Schema();
			sh4.required('first_name');
			sh4.field('last_name');
			sh4.optional('middle_name');

		test.deepEqual(sh1, sh2);
		test.deepEqual(sh1, sh3);
		test.deepEqual(sh1, sh4);

		test.done();
	},

	'Simple Usage: verify - no specified fields': function (test) {
		var sh1 = new Schema().object(function () {
			this.field('first_name');
			this.required('last_name');
			this.optional('middle_name');
		});

		var value = {};
		sh1.verify(value, function (err, isValid, vError) {
			test.ok(!isValid);
			test.equal(vError.ruleName, 'required');
			test.strictEqual(vError.ruleParams, null);
			test.strictEqual(vError.value, undefined);
			test.deepEqual(vError.path, ['first_name']);
			test.done(err);
		});
	},

	'Simple Usage: verify - optional field isn\'t specified': function (test) {
		var sh1 = new Schema().object(function () {
			this.field('first_name');
			this.required('last_name');
			this.optional('middle_name');
		});

		var value = { first_name: 'hello', last_name: 'world' };
		sh1.verify(value, function (err, isValid, vError) {
			test.ok(isValid);
			test.strictEqual(vError, null);
			test.done(err);
		});
	},

	'Simple Usage: verify - correct if all available fields not empty': function (test) {
		var sh1 = new Schema().object(function () {
			this.field('first_name');
			this.required('last_name');
			this.optional('middle_name');
		});

		var value = { first_name: 'hello', last_name: 'world', middle_name: 'param-pam-pam' };
		sh1.verify(value, function (err, isValid, vError) {
			test.ok(isValid);
			test.strictEqual(vError, null);
			test.done(err);
		});
	},

	'Simple Usage: verify - excess field': function (test) {
		var sh1 = new Schema().object(function () {
			this.field('first_name');
			this.required('last_name');
			this.optional('middle_name');
		});

		var value = {
			first_name: 'hello',
			last_name: 'world',
			middle_name: 'param-pam-pam'
		};
		var _keys = _.keys(value);
		value.excess_field = true;

		sh1.verify(value, function (err, isValid, vError) {
			test.ok(!isValid);
			test.equal(vError.ruleName, 'available_fields');
			test.deepEqual(vError.ruleParams, _keys);
			test.deepEqual(vError.value, value);
			test.deepEqual(vError.path, []);
			test.done(err);
		});
	},

	'Create Schema, Register - equal registering': function (test) {
		var sh1 = new Schema();
		var sh2 = Schema(); // jshint ignore: line
		var sh3 = new Schema('nameForRegister1');
		var sh4 = Schema('nameForRegister2'); // jshint ignore: line
		Schema.register('someSchema1', sh1);
		Schema.register('someSchema2', sh2);

		test.strictEqual(Schema.get('someSchema1'), sh1);
		test.strictEqual(Schema.get('someSchema2'), sh2);
		test.strictEqual(Schema.get('nameForRegister1'), sh3);
		test.strictEqual(Schema.get('nameForRegister2'), sh4);

		test.done();
	},

	'Object Schema Building: attachTo': function (test) {
		var sh1_1 = new Schema().object(function () {
			this.field('hello');
			this.field('world');
		});

		var sh1_2 = new Schema('attachExampleSchema').object(function () {
			this.field('hello');
			this.field('world');
		});

		var sh2 = new Schema().array(function () {
			this.field('some');
		});


		// ATTACH
		sh2.clone().attachTo(sh1_1, 'myField');
		sh2.clone().attachTo('attachExampleSchema', 'myField');


		// EQUAL
		var sh3 = new Schema('Hello').object(function () {
			this.field('hello');
			this.field('world');
			this.field('myField').array(function () {
				this.field('some');
			});
		});

		test.deepEqual(sh1_1, sh3);
		test.deepEqual(sh1_2, sh3);

		test.done();
	},
};
{'fraction_non_alphanumeric': 0.18078512396694216, 'fraction_numerical': 0.012138429752066115, 'mean_word_length': 4.159893404397069, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2832861', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2627, 'n_words': 518}