Skip to content

buildFromProperties

buildFromProperties(schema, creatorAddress): ISchemaAccountsDetails

Constructs a schema object from specified properties, assigning unique identifiers and ensuring compliance with schema standards. This function simplifies schema creation by generating a structured schema with the appropriate metadata, making it ready for use in validation, storage, or transmission.

Functionality:

  1. Schema Creation and Metadata Assignment: The input properties are used to construct the schema object, with additional metadata like $schema and additionalProperties flags set according to SchemaModelV1.
  2. URI and Digest Generation: A unique URI and digest are computed for the schema content using getUriForSchema. This ensures the schema is uniquely identifiable and tamper-proof.
  3. DID-based Traceability: The creator’s address is converted into a DID-compliant URI (did:cord:3<address>), facilitating traceability.
  4. Schema Verification: The constructed schema is verified for structure and consistency using verifySchemaStructure.

Parameters:

Parameters

schema: ISchema

An object defining the structure, properties, and constraints of the schema. It conforms to the ISchema interface and serves as the foundation for the final schema object.

creatorAddress: string

The blockchain address of the schema’s creator. This address is formatted into a DID URI, ensuring the creator’s identity is associated with the schema.

Returns:

Returns

ISchemaAccountsDetails

  • An object containing:
  • schema: The finalized schema object, including all properties, constraints, and a unique URI.
  • digest: A cryptographic digest of the schema, ensuring data integrity.
  • creatorUri: The creator’s DID URI, enabling identity tracking.

Throws:

Throws

  • If the constructed schema does not meet the required standards or structure, ensuring integrity and compliance.

Example Usage:

const properties = {
title: 'Person',
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' },
},
required: ['name']
};
const creatorAddress = '5F3sa2TJ...'; // Example address
try {
const { schema, digest, creatorUri } = buildFromProperties(properties, creatorAddress);
console.log('Constructed Schema:', schema);
console.log('Schema Digest:', digest);
console.log('Creator URI:', creatorUri);
} catch (error) {
console.error('Error constructing schema:', error);
}

Internal Logic:

  1. Setting Schema Metadata: Ensures additionalProperties is false and $schema points to SchemaModelV1.
  2. Generating URI and Digest: Uses getUriForSchema to derive the URI and digest.
  3. Verifying Schema: Calls verifySchemaStructure to ensure the schema’s correctness.

Source

schema/src/Schema.ts:364