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:
- Schema Creation and Metadata Assignment: The input properties are used to construct the schema object,
with additional metadata like
$schema
andadditionalProperties
flags set according toSchemaModelV1
. - 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. - DID-based Traceability: The creator’s address is converted into a DID-compliant URI (
did:cord:3<address>
), facilitating traceability. - 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
- 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:
- Setting Schema Metadata: Ensures
additionalProperties
is false and$schema
points toSchemaModelV1
. - Generating URI and Digest: Uses
getUriForSchema
to derive the URI and digest. - Verifying Schema: Calls
verifySchemaStructure
to ensure the schema’s correctness.