What is @backstage/catalog-model?
@backstage/catalog-model is a package that provides models and utilities for working with the Backstage catalog. It allows you to define, validate, and manipulate entities within the Backstage ecosystem, such as services, components, and APIs.
What are @backstage/catalog-model's main functionalities?
Entity Validation
This feature allows you to validate entities against the Backstage catalog model. The `validateEntity` function checks if the provided entity conforms to the expected schema.
const { Entity, validateEntity } = require('@backstage/catalog-model');
const entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'my-component',
},
spec: {
type: 'service',
lifecycle: 'production',
owner: 'team-a',
},
};
const validationResult = validateEntity(entity);
console.log(validationResult);
Entity Kind and API Version
This feature allows you to check the kind and API version of an entity. The `Entity.isComponent` function checks if the entity is of kind 'Component'.
const { Entity } = require('@backstage/catalog-model');
const entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'my-component',
},
spec: {
type: 'service',
lifecycle: 'production',
owner: 'team-a',
},
};
console.log(Entity.isComponent(entity)); // true
Entity Relations
This feature allows you to define and manipulate relations between entities. The `relations` field in the entity metadata can be used to specify relationships like ownership.
const { Entity, RELATION_OWNED_BY } = require('@backstage/catalog-model');
const entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'my-component',
relations: [
{
type: RELATION_OWNED_BY,
target: {
kind: 'Group',
name: 'team-a',
namespace: 'default',
},
},
],
},
spec: {
type: 'service',
lifecycle: 'production',
owner: 'team-a',
},
};
console.log(entity.metadata.relations);
Other packages similar to @backstage/catalog-model
ajv
AJV (Another JSON Schema Validator) is a package for validating JSON objects against JSON schemas. It is highly performant and supports JSON Schema draft-07. Compared to @backstage/catalog-model, AJV is more general-purpose and not specifically tailored for Backstage entities.
joi
Joi is a powerful schema description language and data validator for JavaScript. It allows you to define schemas for your data and validate JavaScript objects against these schemas. While Joi is versatile and can be used for various validation tasks, it does not provide the specific entity models and utilities that @backstage/catalog-model offers for Backstage.
yup
Yup is a JavaScript schema builder for value parsing and validation. It is similar to Joi but has a more modern API and is often used with React applications. Like Joi, Yup is a general-purpose validation library and does not include the specific models and utilities for Backstage entities provided by @backstage/catalog-model.