Skip to content

Commit

Permalink
Add support for validating single content files
Browse files Browse the repository at this point in the history
  • Loading branch information
javagl committed Oct 12, 2023
1 parent c7e5d9d commit 6960880
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/ValidatorMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ export class ValidatorMain {
config.writeReports,
validationOptions
);
} else if (config.tileContentFile) {
const reportFileName = ValidatorMain.obtainReportFileName(
config,
config.tileContentFile
);
await ValidatorMain.validateTileContentFile(
config.tileContentFile,
reportFileName,
validationOptions
);
} else if (config.metadataSchemaFile) {
const reportFileName = ValidatorMain.obtainReportFileName(
config,
Expand Down Expand Up @@ -172,6 +182,26 @@ export class ValidatorMain {
console.log(` ${numFilesWithInfos} files with infos`);
}

static async validateTileContentFile(
fileName: string,
reportFileName: string | undefined,
options: ValidationOptions | undefined
): Promise<ValidationResult> {
console.log("Validating tile content " + fileName);

const validationResult = await Validators.validateTileContentFile(
fileName,
options
);
if (defined(reportFileName)) {
await writeUnchecked(reportFileName, validationResult.serialize());
} else {
console.log("Validation result:");
console.log(validationResult.serialize());
}
return validationResult;
}

static async validateSchemaFile(
fileName: string,
reportFileName: string | undefined
Expand Down
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const args = yargs(process.argv.slice(1))
"of the report will be derived from the input file name, " +
"and be written into the same directory as the input file.",
},
tileContentFile: {
type: "string",
alias: "f",
describe: "The tile content input file path",
},
optionsFile: {
type: "string",
alias: "o",
Expand Down
69 changes: 66 additions & 3 deletions src/validation/Validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import path from "path";
import fs from "fs";

import { defined } from "3d-tiles-tools";
import { LazyContentData } from "3d-tiles-tools";
import { Buffers } from "3d-tiles-tools";

import { ResourceResolvers } from "3d-tiles-tools";
import { TileImplicitTiling } from "3d-tiles-tools";

import { Validator } from "./Validator";
import { TilesetValidator } from "./TilesetValidator";
Expand All @@ -15,11 +16,10 @@ import { ValidationState } from "./ValidationState";
import { ValidationOptions } from "./ValidationOptions";
import { ExtendedObjectsValidators } from "./ExtendedObjectsValidators";
import { TilesetPackageValidator } from "./TilesetPackageValidator";
import { ContentDataValidators } from "./ContentDataValidators";

import { SchemaValidator } from "./metadata/SchemaValidator";

import { TileImplicitTiling } from "3d-tiles-tools";

import { IoValidationIssues } from "../issues/IoValidationIssue";
import { ContentValidationIssues } from "../issues/ContentValidationIssues";

Expand Down Expand Up @@ -175,6 +175,69 @@ export class Validators {
return context.getResult();
}

/**
* Performs a default validation of the given tile content file, and
* returns a promise to the `ValidationResult`.
*
* The given file may be of any format. The method will detect
* the format, and decide whether it can perform a sensible
* validation, based on the existing validation functionality.
*
* @param filePath - The file path
* @param validationOptions - The `ValidationOptions`. When this
* is not given (or `undefined`), then default validation options
* will be used. See {@link ValidationOptions}.
* @returns A promise to a `ValidationResult` that is fulfilled when
* the validation finished.
* @beta
*/
static async validateTileContentFile(
filePath: string,
validationOptions?: ValidationOptions
): Promise<ValidationResult> {
const directory = path.dirname(filePath);
const fileName = path.basename(filePath);
const resourceResolver =
ResourceResolvers.createFileResourceResolver(directory);
const context = new ValidationContext(
directory,
resourceResolver,
validationOptions
);
const contentData = new LazyContentData(fileName, resourceResolver);

// Check if the file exists, and bail out early if it doesn't
const dataExists = await contentData.exists();
if (!dataExists) {
const message = `Content file ${filePath} could not be resolved`;
const issue = ContentValidationIssues.CONTENT_VALIDATION_ERROR(
filePath,
message
);
context.addIssue(issue);
return context.getResult();
}

// Find the validator for the content data, and bail
// out early if none could be found
const dataValidator = await ContentDataValidators.findContentDataValidator(
contentData
);
if (!defined(dataValidator)) {
const message = `No valid content type could be determined for ${filePath}`;
const issue = ContentValidationIssues.CONTENT_VALIDATION_WARNING(
filePath,
message
);
context.addIssue(issue);
return context.getResult();
}

// Perform the actual validation
await dataValidator.validateObject(fileName, contentData, context);
return context.getResult();
}

/**
* Creates a `SchemaValidator` with an unspecified default configuration.
*
Expand Down

0 comments on commit 6960880

Please sign in to comment.