<commit_before>/**
 * this plugin validates documents before they can be inserted into the RxCollection.
 * It's using z-schema as jsonschema-validator
 * @link https://github.com/zaggino/z-schema
 */
import ZSchema from 'z-schema';
import {
    newRxError
} from '../rx-error';
import {
    requestIdleCallbackIfAvailable
} from '../util';

/**
 * cache the validators by the schema-hash
 * so we can reuse them when multiple collections have the same schema
 * @type {Object<string, any>}
 */
const validatorsCache = {};


/**
 * returns the parsed validator from z-schema
 * @param {string} [schemaPath=''] if given, the schema for the sub-path is used
 * @
 */
function _getValidator() {
    const schemaPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    const hash = this.hash;
    if (!validatorsCache[hash]) validatorsCache[hash] = {};
    const validatorsOfHash = validatorsCache[hash];
  
    if (!validatorsOfHash[schemaPath]) {
        const schemaPart = schemaPath === '' ? this.jsonID : this.getSchemaByObjectPath(schemaPath);
    
        if (!schemaPart) {
            throw newRxError('VD1', {
                schemaPath: schemaPath
            });
        }
  
        const validator = new ZSchema();
        validatorsOfHash[schemaPath] = (obj) => {
            validator.validate(obj, schemaPart);
            return validator;
        };
    }
  
    return validatorsOfHash[schemaPath];
}

/**
 * validates the given object against the schema
 * @param  {any} obj
 * @param  {String} [schemaPath=''] if given, the sub-schema will be validated
 * @throws {RxError} if not valid
 * @return {any} obj if validation successful<commit_msg>FIX default arguments in validate-z-schema<commit_after>/**
 * this plugin validates documents before they can be inserted into the RxCollection.
 * It's using z-schema as jsonschema-validator
 * @link https://github.com/zaggino/z-schema
 */
import ZSchema from 'z-schema';
import {
    newRxError
} from '../rx-error';
import {
    requestIdleCallbackIfAvailable
} from '../util';

/**
 * cache the validators by the schema-hash
 * so we can reuse them when multiple collections have the same schema
 * @type {Object<string, any>}
 */
const validatorsCache = {};


/**
 * returns the parsed validator from z-schema
 * @param {string} [schemaPath=''] if given, the schema for the sub-path is used
 * @
 */
function _getValidator(rxSchema, schemaPath = '') {
    const hash = rxSchema.hash;
    if (!validatorsCache[hash]) validatorsCache[hash] = {};
    const validatorsOfHash = validatorsCache[hash];
  
    if (!validatorsOfHash[schemaPath]) {
        const schemaPart = schemaPath === '' ? this.jsonID : this.getSchemaByObjectPath(schemaPath);
    
        if (!schemaPart) {
            throw newRxError('VD1', {
                schemaPath: schemaPath
            });
        }
  
        const validator = new ZSchema();
        validatorsOfHash[schemaPath] = (obj) => {
            validator.validate(obj, schemaPart);
            return validator;
        };
    }
  
    return validatorsOfHash[schemaPath];
}

/**
 * validates the given object against the schema
 * @param  {any} obj
 * @param  {String} [schemaPath=''] if given, the sub-schema will be validated
 * @throws {RxError} if not valid
 * @return {any} obj if validation successful
{'fraction_non_alphanumeric': 0.12009583707696915, 'fraction_numerical': 0.001497454327643007, 'mean_word_length': 3.019253910950662, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 2, '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': '4368643', 'n_tokens_mistral': 959, 'n_tokens_neox': 949, 'n_words': 291}