RAMEN Core
    Preparing search index...

    Validate RAMEN Data

    Validation checks two things: whether the project model is valid, and whether recorded data follow it. RAMEN adds annotation rules to RMF validation. Problems appear as diagnostics.

    Continue from the Media model, where person can annotate Text or Image and must refer to one Person:

    import type { ProjectValidationResult } from '@ramen-suite/core/model';

    const modelResult: ProjectValidationResult = media.validate();
    if (!modelResult.valid) {
    console.log(modelResult.diagnostics);
    }

    validate() checks the model definitions before export or data validation.

    Use validateAnnotationValue() for a property of one annotation shape. Continue from Add Properties, where role.name uses the RoleName value list:

    import type { ValidationResult } from '@ramen-suite/core/model';

    const valid: ValidationResult = media.validateAnnotationValue(RAMEN_ELEMENT.ANNOTATION, 'role', 'name', 'sender');
    const invalid: ValidationResult = media.validateAnnotationValue(RAMEN_ELEMENT.ANNOTATION, 'role', 'name', 'editor');

    console.log(valid.hasErrors); // false
    console.log(invalid.diagnostics); // INSTANCE_PROPERTY_LITERAL_INVALID

    This checks the property's bounds, data type and constraints, including inherited annotation shapes. An unavailable type or property throws an error. You can pass a ConstraintRegistry as the last argument for custom evaluators. Links and uniqueness across records need a complete data graph.

    A tool supplies its records as a ModelInstanceGraph. This example has one Text record, one person annotation and one Person record. Record and link IDs contain at least 16 characters:

    import type { Diagnostic, ModelInstanceGraph, ValidationResult } from '@ramen-suite/core/model';
    import { RAMEN_MODEL } from '@ramen-suite/core/ramen';

    const data: ModelInstanceGraph = {
    nodes: [
    {
    id: 'textRecord000001',
    typeId: text.id,
    properties: { uuid: crypto.randomUUID() },
    },
    {
    id: 'annotationRecord',
    typeId: `${RAMEN_MODEL.id}:${RAMEN_ELEMENT.ANNOTATION}`,
    properties: {
    uuid: crypto.randomUUID(),
    type: 'person',
    selector: { type: 'TextPositionSelector', start: 4, end: 9 },
    },
    },
    {
    id: 'personRecord0001',
    typeId: person.id,
    properties: { uuid: crypto.randomUUID(), label: 'Alex Morgan' },
    },
    ],
    relations: [
    { id: 'textLinkRecord01', typeId: textLink.id, fromId: 'textRecord000001', toId: 'annotationRecord' },
    { id: 'personLinkRecord', typeId: personLink.id, fromId: 'annotationRecord', toId: 'personRecord0001' },
    ],
    };

    const result: ValidationResult = media.validateInstances(data);
    console.log(result.diagnostics);

    Remove the Person link and check again:

    const withoutReference: ModelInstanceGraph = { ...data, relations: data.relations.slice(0, 1) };
    const invalid: ValidationResult = media.validateInstances(withoutReference);

    console.log(invalid.diagnostics.map((item: Diagnostic): string => item.code));
    // ['RAMEN_INSTANCE_ANNOTATION_RELATION_CARDINALITY']

    typeId points to a model definition, while properties.type chooses the annotation shape. The graph needs no model ID because media supplies it.

    A tool translates its own records into ModelInstanceGraph before validation. RAMEN checks annotation shapes, allowed properties and links, and that selector has a type. The tool interprets selector details such as start and end. See Add Properties for fixed value lists and Add Constraints for custom checks.