Skip to content

API Reference ​

Complete API documentation for VZparse.

VZ Class ​

The main VZ class provides parsing, validation, and utility methods.

Methods ​

VZ.parse(input, options?) ​

Parse a VZ string and return the parsed data. Throws an error if parsing fails.

javascript
import { VZ } from 'vzparse';

const data = VZ.parse(`
  app: {
    name: "MyApp"
    version: "1.0.0"
  }
`);

console.log(data.app.name); // "MyApp"

Parameters:

  • input (string): The VZ string to parse
  • options (ParseOptions, optional): Parsing options

Returns: VZObject - The parsed data

Throws: Error if parsing fails

VZ.safeParse(input, options?) ​

Safely parse a VZ string without throwing errors.

javascript
import { VZ } from 'vzparse';

const result = VZ.safeParse(`
  app: {
    name: "MyApp"
    version: "1.0.0"
  }
`);

if (result.success) {
  console.log(result.data.app.name); // "MyApp"
} else {
  console.log(result.errors); // Array of errors
}

Parameters:

  • input (string): The VZ string to parse
  • options (ParseOptions, optional): Parsing options

Returns: ParseResult - Object with success flag, data, and errors

VZ.parseFile(filePath, options?) ​

Parse a VZ file from the filesystem.

javascript
import { VZ } from 'vzparse';

const data = VZ.parseFile('./config.vz');
console.log(data);

Parameters:

  • filePath (string): Path to the VZ file
  • options (ParseOptions, optional): Parsing options

Returns: VZObject - The parsed data

VZ.validate(data, schema) ​

Validate data against a schema.

javascript
import { VZ } from 'vzparse';

const schema = VZ.schema({
  id: { type: 'int', required: true },
  name: { type: 'string', required: true }
});

const isValid = VZ.validate({ id: 1, name: "John" }, schema);
console.log(isValid); // true

Parameters:

  • data (VZObject): Data to validate
  • schema (VZSchema): Schema definition

Returns: boolean - Whether the data is valid

VZ.getValidationErrors(data, schema) ​

Get detailed validation errors.

javascript
import { VZ } from 'vzparse';

const schema = VZ.schema({
  id: { type: 'int', required: true },
  name: { type: 'string', required: true }
});

const errors = VZ.getValidationErrors({ id: "invalid" }, schema);
console.log(errors); // Array of validation errors

Parameters:

  • data (VZObject): Data to validate
  • schema (VZSchema): Schema definition

Returns: ParseError[] - Array of validation errors

VZ.stringify(data, indent?) ​

Convert a JavaScript object back to VZ format.

javascript
import { VZ } from 'vzparse';

const data = {
  app: {
    name: "MyApp",
    version: "1.0.0"
  }
};

const vzString = VZ.stringify(data, 2);
console.log(vzString);

Parameters:

  • data (VZObject): Data to stringify
  • indent (number, optional): Indentation level (default: 2)

Returns: string - VZ formatted string

VZ.schema(definition) ​

Create a schema definition helper.

javascript
import { VZ } from 'vzparse';

const schema = VZ.schema({
  id: { type: 'int', required: true },
  name: { type: 'string', required: true },
  email: { type: 'string', format: 'email' }
});

Parameters:

  • definition (VZSchema): Schema definition

Returns: VZSchema - Schema object

VZEnhanced Class ​

Extended VZ class with additional features.

Methods ​

VZEnhanced.parseWithDetails(input, options?) ​

Parse with detailed error reporting including source context.

javascript
import { VZEnhanced } from 'vzparse';

const result = VZEnhanced.parseWithDetails(`
  schema: {
    id: "int!"
    name: "string!"
  }
  
  id: "not a number"
  name: "John"
`);

if (!result.success) {
  result.errors?.forEach(error => {
    console.log(error.context); // Detailed error context
  });
}

VZEnhanced.parseMultilineWithMetadata(token) ​

Extract metadata from multiline string tokens.

javascript
import { VZEnhanced } from 'vzparse';

const token = { type: 1, value: "markdown\n# Heading\nContent", line: 0, column: 0 };
const result = VZEnhanced.parseMultilineWithMetadata(token);

console.log(result.metadata); // "markdown"
console.log(result.content); // "# Heading\nContent"

VZEnhanced.validateWithReport(data, schema) ​

Validate data and get detailed report with warnings.

javascript
import { VZEnhanced } from 'vzparse';

const data = {
  id: 1,
  name: "John",
  extra: "This field is not in schema"
};

const schema = VZ.schema({
  id: { type: 'int', required: true },
  name: { type: 'string', required: true }
});

const report = VZEnhanced.validateWithReport(data, schema);
console.log(report.valid); // true
console.log(report.errors); // []
console.log(report.warnings); // ["Extra field not in schema: extra"]

VZEnhanced.prettyPrint(data, options?) ​

Pretty print VZ data with colors (for CLI).

javascript
import { VZEnhanced } from 'vzparse';

const data = {
  app: {
    name: "MyApp",
    version: "1.0.0"
  }
};

console.log(VZEnhanced.prettyPrint(data, { colors: true, indent: 2 }));

Types ​

ParseOptions ​

typescript
interface ParseOptions {
  basePath?: string;           // Base path for imports
  allowImports?: boolean;      // Allow import statements
  resolveTemplates?: boolean;  // Resolve template strings
  executeQueries?: boolean;    // Execute embedded queries
  context?: VZObject;         // Context for template resolution
}

ParseResult ​

typescript
interface ParseResult {
  success: boolean;           // Whether parsing succeeded
  data?: VZObject;           // Parsed data
  schema?: VZSchema;         // Extracted schema
  errors?: ParseError[];     // Parse errors
}

ParseError ​

typescript
interface ParseError {
  line: number;              // Line number
  column: number;            // Column number
  path: string;              // Path to the error
  message: string;           // Error message
  context?: string;          // Error context
}

VZSchema ​

typescript
interface VZSchema {
  [key: string]: SchemaType;
}

type SchemaType = {
  type: "string" | "int" | "float" | "bool" | "datetime" | "array" | "object" | "enum";
  required?: boolean;
  validator?: (value: VZValue) => boolean;
  message?: string;
  arrayOf?: SchemaType;
  objectSchema?: VZSchema;
  enumValues?: string[];
  min?: number;
  max?: number;
  minLength?: number;
  maxLength?: number;
  pattern?: RegExp;
  format?: "url" | "email" | "uuid" | "date" | "datetime";
};

VZValue ​

typescript
type VZValue =
  | string
  | number
  | boolean
  | null
  | VZObject
  | VZArray
  | EmbeddedQuery
  | TemplateString;

type VZObject = { [key: string]: VZValue };
type VZArray = VZValue[];

EmbeddedQuery ​

typescript
interface EmbeddedQuery {
  __type: "embedded_query";
  source: string;             // Data source identifier
  query: string;             // Query expression
}

TemplateString ​

typescript
interface TemplateString {
  __type: "template";
  template: string;          // Template string
  variables: string[];       // Variable names
}

Released under the MIT License.