@tfw.in/structura-sdk
v0.1.0
Published
TypeScript SDK for Saral Structura, providing Zod schemas and validation for document processing outputs.
Maintainers
Readme
TypeScript SDK for Saral Document Validation
This SDK provides Zod schemas and a validator function to parse and validate JSON data structured according to the Saral DocumentOutput format. It's designed to ensure that your document data conforms to the expected schema before further processing.
Installation
To install the SDK, use npm or yarn:
npm install @tfw.in/structura/sdkor
yarn add @tfw.in/structura/sdkBasic Usage
The primary function provided by this SDK is parseAndValidateDocumentOutput. You can use it to validate your JSON data.
import { parseAndValidateDocumentOutput, ValidationResult, DocumentOutput } from '@tfw.in/structura/sdk';
// Assuming jsonData is a string containing your JSON data
const jsonData: string = '{"version": "0.0.1", "blocks": [...], ...}';
const result: ValidationResult = parseAndValidateDocumentOutput(jsonData);
if (result.isValid) {
console.log("Validation successful!");
const documentOutput: DocumentOutput | null = result.data;
// Use documentOutput for further processing
if (documentOutput) {
console.log("Document Version:", documentOutput.version);
}
} else {
console.error("Validation failed:");
result.errors?.forEach(error => console.error("- ", error));
}Available Exports
This SDK exports the following:
Validator Function
parseAndValidateDocumentOutput(jsonData: string): ValidationResult
Interfaces
ValidationResult:{ data: DocumentOutput | null; isValid: boolean; errors: string[]; }
Zod Schemas and Inferred Types
The SDK exports various Zod schemas and their corresponding TypeScript types. These are the source of truth for the data structures.
BlockTypesEnumSchema&BlockTypesEnumType(Zod schema and its inferred type for theBlockTypesenum)PolygonBoxSchema&PolygonBoxBlockIdSchema&BlockIdBlockMetadataSchema&BlockMetadataBlockSchema&Block(Base block schema)TableCellSchema&TableCellMergedTableSchema&MergedTableBlockOutputSchema&BlockOutput(Schema for individual blocks within the document output)DocumentOutputSchema&DocumentOutput(Top-level schema for the entire document output)
Enums
BlockTypes(The TypeScript enum itself)
You can import these directly as needed:
import {
DocumentOutputSchema,
DocumentOutput,
BlockOutputSchema,
BlockOutput,
BlockTypes,
parseAndValidateDocumentOutput,
ValidationResult
} from '@tfw.in/structura/sdk';Development
Prerequisites
- Node.js (version specified in
package.jsonor higher) - Yarn or npm
Setup
- Clone the repository.
- Navigate to the
sdk/typescriptdirectory. - Install dependencies:
ornpm installyarn install
Running Tests
To run the test suite:
npm testor
yarn testThis will execute the tests defined in the tests/ directory using Jest.
Building
To build the TypeScript code (compiles to the dist directory):
npm run tsc # Assuming you add a script like "tsc": "tsc" to package.json
# or directly
npx tsc(You might want to add a build script to your package.json like "build": "tsc")
Publishing to NPM (Maintainer Notes)
- Ensure
package.jsonis correctly configured (name, version, main, types, files, repository, author, license, etc.). - Build the project:
npm run build(ornpx tsc). - Login to npm:
npm login. - Publish:
npm publish.
Remember to increment the version in package.json before publishing updates.
