TypeScript SDK for Saral Structura, providing Zod schemas and validation for document processing outputs.
npm install @tfw.in/structura-sdkThis 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.
To install the SDK, use npm or yarn:
``bash`
npm install @tfw.in/structura/sdk
or
`bash`
yarn add @tfw.in/structura/sdk
The primary function provided by this SDK is parseAndValidateDocumentOutput. You can use it to validate your JSON data.
`typescript
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));
}
`
This SDK exports the following:
$3
- ValidationResult: { data: DocumentOutput | null; isValid: boolean; errors: string[]; }$3
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 the BlockTypes enum)
- PolygonBoxSchema & PolygonBox
- BlockIdSchema & BlockId
- BlockMetadataSchema & BlockMetadata
- BlockSchema & Block (Base block schema)
- TableCellSchema & TableCell
- MergedTableSchema & MergedTable
- BlockOutputSchema & BlockOutput (Schema for individual blocks within the document output)
- DocumentOutputSchema & DocumentOutput (Top-level schema for the entire document output)$3
- BlockTypes (The TypeScript enum itself)You can import these directly as needed:
`typescript
import {
DocumentOutputSchema,
DocumentOutput,
BlockOutputSchema,
BlockOutput,
BlockTypes,
parseAndValidateDocumentOutput,
ValidationResult
} from '@tfw.in/structura/sdk';
`Development
$3
- Node.js (version specified in package.json or higher)
- Yarn or npm$3
1. Clone the repository.
2. Navigate to the sdk/typescript directory.
3. Install dependencies:
`bash
npm install
`
or
`bash
yarn install
`$3
To run the test suite:
`bash
npm test
`
or
`bash
yarn test
`
This will execute the tests defined in the tests/ directory using Jest.Building
To build the TypeScript code (compiles to the dist directory):
`bash
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)
1. Ensure package.json is correctly configured (name, version, main, types, files, repository, author, license, etc.).
2. Build the project: npm run build (or npx tsc).
3. Login to npm: npm login.
4. Publish: npm publish.Remember to increment the version in
package.json` before publishing updates.