Skip to content

Commit

Permalink
added common and validate.test files from my test branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ryma2fhir committed Jan 22, 2024
1 parent 00422af commit 0ff3390
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 48 deletions.
105 changes: 61 additions & 44 deletions src/common.js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,36 +377,52 @@ export function testFileWarning(testDescription, file,message) {
});
}

export function isIgnoreFolder(folderName : string) : boolean {

export function isIgnoreFolder(folderName: string): boolean {
const optionsPath = '../options.json';

if (folderName.startsWith('.')) return true;
if (folderName == 'node_modules') return true;
if (folderName == 'Diagrams') return true;
if (folderName == 'Diagams') return true;
if (folderName == 'diagrams') return true;
if (folderName == 'FML') return true;
if (folderName == 'dist') return true;
if (folderName == 'documents') return true;
if (folderName == 'nhsdtheme') return true;
if (folderName == 'ukcore') return true;
if (folderName == 'UKCore') return true;
if (folderName == 'apim') return true;
if (folderName == 'Supporting Information') return true;
// This project needs to avoid these folders
if (folderName == 'validation') return true;
if (folderName == 'validation-service-fhir-r4') return true;
// For BARS
if (folderName == 'guides') return true;
try {
const optionsContent = fs.readFileSync(optionsPath, 'utf-8');
const options = JSON.parse(optionsContent);

if (options['ignore-folders']) {
if (options['ignore-folders'].includes(folderName)) {
return true;
} else {
return false;
}
} else {
console.log('Warning: "ignore-folders" attribute not found in options.json');
}
} catch (error) {
console.error('Error reading options.json:', error.message);
}

return false;
}

export function isIgnoreFile(directory : string, fileName : string) : boolean {
export function isIgnoreFile(directory: string, fileName: string): boolean {
var fileExtension = fileName.split('.').pop().toUpperCase();
let file = directory +'/'+ fileName
if (fileName == 'fhirpkg.lock.json') return true;
if (fileName == 'package.json') return true;
let file = directory + '/' + fileName;

// Read options from options.json
let ignoreFiles: string[] = [];
try {
const optionsFile = fs.readFileSync('../options.json', 'utf8');
const options = JSON.parse(optionsFile);
ignoreFiles = options['ignore-files'] || [];

if (!options.hasOwnProperty('ignore-files')) {
console.log('Warning: The "ignore-files" attribute is missing in options.json');
}
} catch (e) {
console.error('Error reading options.json:', (e as Error).message);
}

if (ignoreFiles.includes(fileName)) return true;

if (fileExtension == 'JSON' || fileExtension == 'XML') {
let json = undefined
let json = undefined;
if (directory.indexOf('FHIR') > 0) return false;
try {
json = JSON.parse(getJson(file, fs.readFileSync(file, 'utf8')))
Expand All @@ -417,27 +433,28 @@ export function isIgnoreFile(directory : string, fileName : string) : boolean {
} catch (e) {
console.info('Ignoring file ' + file + ' Error message ' + (e as Error).message)
}
} return true;
}

export function isDefinition(fileNameOriginal : string) : boolean {
// console.info(fileNameOriginal);
let fileName = fileNameOriginal.toUpperCase();

if (fileName.startsWith('CapabilityStatement'.toUpperCase())) return true;
if (fileName.startsWith('ConceptMap'.toUpperCase())) return true;
if (fileName.startsWith('CodeSystem'.toUpperCase())) return true;
if (fileName.startsWith('MessageDefinition'.toUpperCase())) return true;
if (fileName.startsWith('NamingSystem'.toUpperCase())) return true;
if (fileName.startsWith('ObservationDefinition'.toUpperCase())) return true;
if (fileName.startsWith('OperationDefinition'.toUpperCase())) return true;
if (fileName.startsWith('Questionnaire'.toUpperCase())) return true;
if (fileName.startsWith('SearchParameter'.toUpperCase())) return true;
if (fileName.startsWith('StructureDefinition'.toUpperCase())) return true;
if (fileName.startsWith('ValueSet'.toUpperCase())) return true;
if (fileName.startsWith('StructureMap'.toUpperCase())) return true;
}
return true;
}

return false;
export function isDefinition(fileNameOriginal: string): boolean {
const validPrefixes = [
'CapabilityStatement',
'ConceptMap',
'CodeSystem',
'MessageDefinition',
'NamingSystem',
'ObservationDefinition',
'OperationDefinition',
'Questionnaire',
'SearchParameter',
'StructureDefinition',
'ValueSet',
'StructureMap'
];

const fileName = fileNameOriginal.toUpperCase();
return validPrefixes.some(prefix => fileName.startsWith(prefix.toUpperCase()));
}

export function testFileValidator(testDescription,file) {
Expand Down
32 changes: 28 additions & 4 deletions src/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
getFhirClientJSON, isIgnoreFile, isIgnoreFolder, NEW_LINE, testFile
} from "./common.js";
import * as fs from "fs";
import * as fs from 'fs';
import {describe, expect, jest} from "@jest/globals";
import axios, {AxiosInstance} from "axios";
import * as console from "console";
Expand All @@ -18,10 +18,34 @@ const args = require('minimist')(process.argv.slice(2))
let source = '../'
let examples: string

let failOnWarning = false;
if (process.env.FAILONWARNING != undefined && process.env.FAILONWARNING == 'FAILONWARNING') {
failOnWarning = true;
function readOptionsFile(filePath: string): boolean | undefined {
try {
const data = fs.readFileSync(filePath, 'utf8');
const options = JSON.parse(data);

if (options && typeof options['strict-validation'] === 'boolean') {
return options['strict-validation'];
} else if (!options) {
console.log(`Error: Attribute "strict-validation" not found in ${filePath}.`);
} else {
console.log(`Error: Attribute "strict-validation" is not a boolean in ${filePath}.`);
}
} catch (error) {
console.log(`Error: File ${filePath} not found or invalid JSON.`);
}

return false; // Set to false in case of any error
}

const optionsFilePath = '../options.json';
const failOnWarning = readOptionsFile(optionsFilePath);

if (failOnWarning !== undefined) {
console.log(`failOnWarning: ${failOnWarning}`);
}

console.log(`failOnWarning: ${failOnWarning}`);

gitHubSummary += 'Strict validation: ' + failOnWarning + NEW_LINE;

if (args!= undefined) {
Expand Down

0 comments on commit 0ff3390

Please sign in to comment.