Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
API Reference

Parsing & Validation


This section covers the core functions for reading, parsing, and validating blocklet.yml files. These utilities are the primary entry point for working with blocklet metadata, ensuring that the data is well-formed, complete, and conforms to the official specification before being used by other parts of the system. For a detailed reference on all available fields, please see the Blocklet Specification (blocklet.yml).

parse#

The parse function is the most common and comprehensive utility for loading blocklet metadata. It reads the blocklet.yml (or blocklet.json) file from a specified directory, applies a series of automatic fixes and normalizations, and validates the final object against the blocklet schema.

Process Flow#

The function follows a multi-step process to ensure robust and consistent metadata:


Parameters#

Name

Type

Description

dir

string

Required. The root directory of the blocklet project containing the blocklet.yml file.

options

object

Optional. An object containing configuration options.

options.ensureFiles

boolean

If true, verifies that files listed in the logo and files fields actually exist on the filesystem. Defaults to false.

options.ensureDist

boolean

If true, requires the dist field to be present and valid in the metadata. Defaults to false.

options.ensureComponentStore

boolean

If true, ensures that any components sourced from a store have a source.store URL specified. Defaults to true.

options.extraRawAttrs

object

An object with additional attributes to merge into the metadata before fixing and validation. Useful for registries to inject data. Defaults to {}.

options.defaultStoreUrl

string or function

A URL to be used as the default source.store for components that don't specify one.

options.fix

boolean

If true, applies all automatic normalization and cleanup functions. Defaults to true.

Returns#

A fully validated and normalized TBlockletMeta object. If parsing, fixing, or validation fails, the function throws an Error with a descriptive message.

Example#

import path from 'path';
import { parse } from '@blocklet/meta';

const blockletDir = path.join(__dirname, 'my-blocklet-project');

try {
const meta = parse(blockletDir, { ensureFiles: true });
console.log('Successfully parsed blocklet meta for:', meta.title);
console.log('Version:', meta.version);
} catch (error) {
console.error('Failed to parse blocklet.yml:', error.message);
}

validateMeta#

Use validateMeta when you already have a blocklet metadata object in memory (e.g., from an API response or programmatically constructed) and need to validate it against the blocklet schema.

Parameters#

Name

Type

Description

meta

any

Required. The blocklet metadata object to be validated.

options

object

Optional. An object containing configuration options for validation.

options.ensureFiles

boolean

See parse options. Defaults to false.

options.ensureDist

boolean

See parse options. Defaults to false.

options.ensureComponentStore

boolean

See parse options. Defaults to true.

options.ensureName

boolean

If true, requires the name field to exist. Defaults to false.

options.skipValidateDidName

boolean

If true, skips the validation that ensures the did corresponds to the name. Used in specific server-side scenarios. Defaults to false.

Returns#

A validated TBlockletMeta object. Throws an Error if the object is invalid.

Example#

import { validateMeta } from '@blocklet/meta';

const myMeta = {
did: 'z8iZqD4F2j4a7b5c6d7e8f9g0h1i2j3k4l5m6n7o8p9q', // Must be a valid DID
name: 'z8iZqD4F2j4a7b5c6d7e8f9g0h1i2j3k4l5m6n7o8p9q',
version: '1.0.0',
title: 'My Custom Blocklet',
description: 'A programmatically created blocklet meta.',
main: 'dist',
};

try {
const validatedMeta = validateMeta(myMeta);
console.log('Meta is valid:', validatedMeta.title);
} catch (error) {
console.error('Meta validation failed:', error.message);
}

fixAndValidateService#

This is a specialized utility for fixing and validating the service configurations within a blocklet's interfaces. It is called internally by parse but can be used standalone if you need to process service definitions separately.

Parameters#

Name

Type

Description

meta

TBlockletMeta

Required. A blocklet metadata object that may contain interfaces with services.

Returns#

The input TBlockletMeta object with its service configurations validated and potentially modified with defaults.

Example#

import { fixAndValidateService } from '@blocklet/meta';

const metaWithService = {
did: 'z8iZqD4F2j4a7b5c6d7e8f9g0h1i2j3k4l5m6n7o8p9q',
name: 'z8iZqD4F2j4a7b5c6d7e8f9g0h1i2j3k4l5m6n7o8p9q',
version: '1.0.0',
description: 'A blocklet with a service.',
main: 'dist',
interfaces: [
{
type: 'web',
name: 'publicUrl',
path: '/',
prefix: '*',
protocol: 'http',
services: [
{
name: '@abtnode/auth-service',
config: {
whoCanAccess: 'all',
profileFields: ['fullName', 'email']
}
}
]
}
]
};

try {
const fixedMeta = fixAndValidateService(metaWithService);
console.log('Service config validated:', fixedMeta.interfaces[0].services[0].config);
} catch (error) {
console.error('Service validation failed:', error);
}

Automatic Fixes#

The parse function (when fix: true) automatically normalizes several fields to reduce boilerplate and ensure consistency. Key fixes include:

  • fixRequired: Adds a default description and version (0.1.0) if they are missing.
  • fixRepository: Converts a string repository URL into a structured object { type: 'git', url: '...' } and normalizes the URL.
  • fixPerson: Parses string-based author, contributors, and maintainers (e.g., 'John Doe <john@example.com> (https://example.com)') into structured objects ({ name, email, url }).
  • fixKeywords / fixTags: Converts a comma-separated string of keywords or tags into an array of strings.
  • fixName: If the did is valid, it sets name to the same value as did.
  • fixInterfaces: Standardizes interface properties, ensuring web interfaces use the http protocol and providing sensible defaults.


Now that you understand how to parse and validate local metadata, continue to the Metadata Helpers section to learn about fetching metadata from remote sources.