<commit_before>import * as fs from 'fs';
import * as ajv from 'ajv';
import { AxiosPromise } from 'axios';
import axios from 'axios';
import { CurrentSchemas, templateSchema, parameterSchema } from './currentSchemas'

export class Validator {

    private loadSchema(uri: string, callback: any) {
        console.log(`download file ${uri}`);
        axios.get(uri)
            .then((response) => {
                console.log(`Load data for ${response.data.id}`);
                callback(null, response.data);
            })
            .catch((error) => {
                console.log(error);
                callback(error || new Error('Loading error: ' + error));
            });
    }

    private validator: ajv.Ajv;
    
    constructor() {
        this.validator = new ajv({ extendRefs: true });
    }

    public Initialize(): Promise<null> {
        var self = this;
        return new Promise((resolve, reject) => {
            var requests: AxiosPromise[] = [];
            CurrentSchemas.forEach(schema => {
                requests.push(axios.get(schema));
            });
            return Promise.all(requests)
                .then(results => {
                    results.forEach(result => {
                        //HACK: until microsoft fix their base schemas.
                        if (result.data.id === "http://schema.management.azure.com/schemas/2014-04-01-preview/Sendgrid.json") {
                            result.data.id = "http://schema.management.azure.com/schemas/2015-01-01/Sendgrid.Email.json";
                        }
                        this.validator.addSchema(result.data);
                        //hack to add https
                        result.data.id = result.data.id.replace("http://", "https://");
                        this.validator.addSchema(result.data);
                    });
                    this.validator.getSchema(templateSchema);
                    this.validator.getSchema(parameterSchema);
                    resolve();
                }).catch(err => {
                    reject(err);
                });
        });
    }

    public validateSchema(filename: string, parameters: any = {}): Promise<Error[]> {
        return new Promise((resolve, reject) => {
            console.log(`Validating schema for ${filename}`);
            var errorFile = this.validateJson(filename);
            if (errorFile) reject(errorFile);
            let current: any = JSON.parse(fs.readFileSync(filename).toString());

            if ('parameters' in current) {
                for (var parameter in parameters) {
                    if (parameters.hasOwnProperty(parameter)) {
                        Object.assign(current.parameters[parameter], parameters[parameter]);
                    }
                }
            }

            this.substituteDefaults(current);
            this.validator.validate(current["$schema"], current);
            resolve(this.validator.errors || []);
        });
    }

    private substituteDefaults(template: any) {
        var parameters = (value: string): any => {
            if (value in template.parameters) {
                return template.parameters[value].value || template.parameters[value].defaultValue || "";
            }
        };
        var variables = (value: string): any => {
            if (value in template.variables) {
                return template.variables[value];
            } else {
                return value;
            }
        };
        var concat = (value: string[]) => {
            return value.join('');
        };
        if (template.variables) {


            Object.keys(template.variables)
                .forEach((variable: string) => {
                    if (typeof template.variables[variable] === 'string' && template.variables[variable].startsWith(`[`)) {
                        var value: string = template.variables[variable].substring(1, template.variables[variable].length - 1);
                        try {
                            template.variables[variable] = eval(value);
                        } catch (err) {
                            template.variables[variable] = value;
                        }
                    }
                });
        }
        if (template.resources) {
            template.resources.forEach((resource: any, i) => {
                Object.keys(resource)
                    .forEach((resourceKey: string) => {
                        if (typeof template.resources[i][resourceKey] === 'string' && template.resources[i][resourceKey].startsWith(`[`)) {
                            var value: string = template.resources[i][resourceKey].substring(1, template.resources[i][resourceKey].length - 1);
                            try {
                                template.resources[i][resourceKey] = eval(value);<commit_msg>Avoid failures due to use of [resourceGroup().location]<commit_after>import * as fs from 'fs';
import * as ajv from 'ajv';
import { AxiosPromise } from 'axios';
import axios from 'axios';
import { CurrentSchemas, templateSchema, parameterSchema } from './currentSchemas'

export class Validator {

    private loadSchema(uri: string, callback: any) {
        console.log(`download file ${uri}`);
        axios.get(uri)
            .then((response) => {
                console.log(`Load data for ${response.data.id}`);
                callback(null, response.data);
            })
            .catch((error) => {
                console.log(error);
                callback(error || new Error('Loading error: ' + error));
            });
    }

    private validator: ajv.Ajv;
    
    constructor() {
        this.validator = new ajv({ extendRefs: true });
    }

    public Initialize(): Promise<null> {
        var self = this;
        return new Promise((resolve, reject) => {
            var requests: AxiosPromise[] = [];
            CurrentSchemas.forEach(schema => {
                requests.push(axios.get(schema));
            });
            return Promise.all(requests)
                .then(results => {
                    results.forEach(result => {
                        //HACK: until microsoft fix their base schemas.
                        if (result.data.id === "http://schema.management.azure.com/schemas/2014-04-01-preview/Sendgrid.json") {
                            result.data.id = "http://schema.management.azure.com/schemas/2015-01-01/Sendgrid.Email.json";
                        }
                        this.validator.addSchema(result.data);
                        //hack to add https
                        result.data.id = result.data.id.replace("http://", "https://");
                        this.validator.addSchema(result.data);
                    });
                    this.validator.getSchema(templateSchema);
                    this.validator.getSchema(parameterSchema);
                    resolve();
                }).catch(err => {
                    reject(err);
                });
        });
    }

    public validateSchema(filename: string, parameters: any = {}): Promise<Error[]> {
        return new Promise((resolve, reject) => {
            console.log(`Validating schema for ${filename}`);
            var errorFile = this.validateJson(filename);
            if (errorFile) reject(errorFile);
            let current: any = JSON.parse(fs.readFileSync(filename).toString());

            if ('parameters' in current) {
                for (var parameter in parameters) {
                    if (parameters.hasOwnProperty(parameter)) {
                        Object.assign(current.parameters[parameter], parameters[parameter]);
                    }
                }
            }

            this.substituteDefaults(current);
            this.validator.validate(current["$schema"], current);
            resolve(this.validator.errors || []);
        });
    }

    private substituteDefaults(template: any) {
        var parameters = (value: string): any => {
            if (value in template.parameters) {
                return template.parameters[value].value || template.parameters[value].defaultValue || "";
            }
        };
        var variables = (value: string): any => {
            if (value in template.variables) {
                return template.variables[value];
            } else {
                return value;
            }
        };
        var concat = (value: string[]) => {
            return value.join('');
        };
        if (template.variables) {


            Object.keys(template.variables)
                .forEach((variable: string) => {
                    if (typeof template.variables[variable] === 'string' && template.variables[variable].startsWith(`[`)) {
                        var value: string = template.variables[variable].substring(1, template.variables[variable].length - 1);
                        try {
                            template.variables[variable] = eval(value);
                        } catch (err) {
                            template.variables[variable] = value;
                        }
                    }
                });
        }
        if (template.resources) {
            template.resources.forEach((resource: any, i) => {

                // Avoids schema failures due to validation of [resourceGroup.location()] against the valid location enum
                function resourceGroup() {
                    return { location: 'West US' };
                }

                Object.keys(resource)
                    .forEach((resourceKey: string) => {
                        if (typeof template.resources[i][resourceKey] === 'string' && template.resources[i][resourceKey].startsWith(`[`)) {
                            var value: string = template.resources[i][resourceKey].substring(1, template.resources[i][resourceKey].length - 1);
                            try {
                                template.resources[i][resourceKey] = eval(value);
{'fraction_non_alphanumeric': 0.12526389866291343, 'fraction_numerical': 0.0040213129586810096, 'mean_word_length': 1.5345222929936306, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '211418', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2329, 'n_words': 474}