@nldoc/types
v4.0.64
Published
NLdoc's Type Definitions for Document Specification
Readme
NLdoc Document Specification Types
TypeScript type definitions and validation schemas for the NLdoc Document Specification. This library provides runtime type validation using Zod schemas, ensuring type safety for document processing and validation.
Overview
This package provides:
- Type Definitions: Complete TypeScript types for all NLdoc document resources
- Runtime Validation: Zod schemas for validating document structures at runtime
- Test Utilities: Helpers for testing document validation and processing
- Resource Types: Comprehensive types for all document elements (Document, Asset, Footnote, etc.)
The types are designed to work seamlessly with the NLdoc Document Specification and provide both compile-time type safety and runtime validation capabilities.
Getting Started
Prerequisites
- Node.js >= 22.0.0 < 23.0.0
- npm >= 10.0.0 < 12.0.0
Installation
Install the package via npm:
npm install @nldoc/typesOr using yarn:
yarn add @nldoc/typesBasic Usage
import { Document, Asset, Footnote } from '@nldoc/types';
// Type-safe document creation
const document: Document = {
type: 'Document',
// ... other document properties
};
// Runtime validation
try {
const validatedDocument = await Document.parseAsync(documentData);
console.log('Document is valid:', validatedDocument);
} catch (error) {
console.error('Validation failed:', error);
}Runtime Validation
The package exports Zod schemas for runtime validation:
import { Document } from '@nldoc/types';
// Validate and parse document data
const result = Document.safeParse(documentData);
if (result.success) {
// TypeScript knows result.data is a valid Document
console.log('Valid document:', result.data);
} else {
// Handle validation errors
console.error('Validation errors:', result.error.issues);
}Development
Local Setup
Clone the repository:
git clone https://gitlab.com/logius/nldoc/lib/typescript/types.git cd typesInstall dependencies:
npm installBuild the project:
npm run build
Available Scripts
npm run test- Run the test suite with Vitest and coveragenpm run test:watch- Run tests in watch mode with coveragenpm run build- Compile TypeScript to JavaScriptnpm run build:check- Type check without emitting filesnpm run lint- Lint the codebase using ESLintnpm run format- Format code using Prettiernpm run format:check- Check code formattingnpm run fix- Auto-fix linting and formatting issuesnpm run commit- Use conventional commits with Commitizen
Type Definitions
The package exports TypeScript types for all NLdoc document resources:
Core Resources
- Document: The root document container
- Asset: External resources (images, files, etc.)
- Footnote: Document footnotes and references
Text Elements
- Heading: Document headings (H1-H6)
- Paragraph: Text paragraphs with inline content
- Text: Plain text content
- Link: Hyperlinks
Structural Elements
- Table: Tables with headers, rows, and cells
- OrderedList/UnorderedList: Numbered and bulleted lists
- DefinitionList: Term-definition pairs
- BlockQuotation: Quoted content blocks
- Preformatted: Pre-formatted text blocks
Media Elements
- Image: Image resources with metadata
Writing New Types
When adding new types, they should be added to src/resources.ts. All types are contained in one file because some have recursive dependencies on others, which prevents breaking them into separate files while maintaining proper import relationships.
See Zod's documentation on recursive types for more information.
Testing
The types are tested against a comprehensive collection of valid and invalid examples from the NLdoc document specification. Test examples are automatically downloaded on first test run.
# Run all tests
npm test
# Run tests in watch mode
npm run test:watchTests validate:
- Type correctness for valid document examples
- Proper rejection of invalid document structures
- Schema validation accuracy
- Cross-reference integrity
Contributing
We welcome contributions! Please ensure:
- All tests pass (
npm test) - Code is properly formatted (
npm run format:check) - Linting rules are followed (
npm run lint) - Type checking passes (
npm run build:check) - Use conventional commits (
npm run commit)
License
This project is licensed under the European Union Public License 1.2 - see LICENSE for details.
Related Packages
- @nldoc/document-specification - OpenAPI 3.1.0 specification
- @nldoc/document-validation-specification - Validation rules
Acknowledgements
- Built with Zod for runtime validation
- Follows OpenAPI 3.1.0 specification standards
- Integrates with Schema.org standards
