Skip to content

Commit

Permalink
#40: expose lib method for schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-s96 committed Oct 11, 2023
1 parent 6fa9efc commit db3257e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
14 changes: 13 additions & 1 deletion lib/schema/generate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { writeFile } = require('./utils');

function createDefinitions(resources, lookups, additionalProperties = false) {
const typeMappings = {
'Edm.String': 'string',
Expand Down Expand Up @@ -226,6 +228,16 @@ function generateSchema(metadataJson, additionalProperties) {
}
}

async function generateJsonSchema({ metadataReportJson = {}, outputFilePath = '', additionalProperties = false }) {
const schema = generateSchema(metadataReportJson, additionalProperties);
if (schema && outputFilePath) {
const success = await writeFile(outputFilePath, JSON.stringify(schema));
if (!success) console.log(`Error writing schema to path ${outputFilePath}`);
}
return schema;
}

module.exports = {
generateSchema
generateSchema,
generateJsonSchema
};
39 changes: 4 additions & 35 deletions lib/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,17 @@

const chalk = require('chalk');
const { promises: fs } = require('fs');
const { generateSchema } = require('./generate');
const { generateSchema, generateJsonSchema } = require('./generate');
const path = require('path');
const { validatePayload, isValidDdVersion } = require('./validate');
const { CURRENT_DATA_DICTIONARY_VERSION } = require('../../common');
const { readDirectory } = require('../restore-utils');
const { getReferenceMetadata } = require('reso-certification-etl');
const { processFiles } = require('./utils');
const { processFiles, createDirectoryIfNotPresent, writeFile, readFile } = require('./utils');

const OUTPUT_DIR = 'reso-schema-validation-temp';
const ERROR_REPORT = 'schema-validation-report.json';

const readFile = async filePath => {
try {
return await fs.readFile(filePath);
} catch (err) {
console.error(`Could not read file from path '${filePath}'! Error: ${err}`);
return null;
}
};

const createDirectoryIfNotPresent = async dirName => {
try {
await fs.stat(dirName);
} catch (error) {
try {
await fs.mkdir(dirName);
} catch (error) {
return null;
}
}
return true;
};

const writeFile = async (path, data) => {
try {
await fs.writeFile(path, data);
return true;
} catch (error) {
console.log(error);
return false;
}
};

const schema = async ({
metadataPath = '',
outputPath = '.',
Expand Down Expand Up @@ -257,5 +225,6 @@ const validatePayloadAndGenerateResults = async ({
};

module.exports = {
schema
schema,
generateJsonSchema
};
37 changes: 36 additions & 1 deletion lib/schema/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ const processFile = async ({ filePath, outputPath }) => {
}
};

const readFile = async filePath => {
try {
return await fs.readFile(filePath);
} catch (err) {
console.error(`Could not read file from path '${filePath}'! Error: ${err}`);
return null;
}
};

const createDirectoryIfNotPresent = async dirName => {
try {
await fs.stat(dirName);
} catch (error) {
try {
await fs.mkdir(dirName);
} catch (error) {
return null;
}
}
return true;
};

const writeFile = async (path, data) => {
try {
await fs.writeFile(path, data);
return true;
} catch (error) {
console.log(error);
return false;
}
};

module.exports = {
processFiles
processFiles,
readFile,
createDirectoryIfNotPresent,
writeFile
};

0 comments on commit db3257e

Please sign in to comment.