Make metadata and name mandatory

This commit is contained in:
Fredrik Adelöw
2020-05-28 17:00:57 +02:00
parent 06879f5542
commit 60e1ad28aa
14 changed files with 102 additions and 100 deletions
+4 -4
View File
@@ -32,9 +32,9 @@ export type Entity = {
kind: string;
/**
* Optional metadata related to the entity.
* Metadata related to the entity.
*/
metadata?: EntityMeta;
metadata: EntityMeta;
/**
* The specification data describing the entity itself.
@@ -86,9 +86,9 @@ export type EntityMeta = {
* The name of the entity.
*
* Must be uniqe within the catalog at any given point in time, for any
* given namespace, for any given kind.
* given namespace + kind pair.
*/
name?: string;
name: string;
/**
* The namespace that the entity belongs to.
@@ -65,23 +65,20 @@ export class FieldFormatEntityPolicy implements EntityPolicy {
require('apiVersion', entity.apiVersion, this.validators.isValidApiVersion);
require('kind', entity.kind, this.validators.isValidKind);
optional(
'metadata.name',
entity.metadata?.name,
this.validators.isValidEntityName,
);
require('metadata.name', entity.metadata.name, this.validators
.isValidEntityName);
optional(
'metadata.namespace',
entity.metadata?.namespace,
entity.metadata.namespace,
this.validators.isValidNamespace,
);
for (const [k, v] of Object.entries(entity.metadata?.labels ?? [])) {
for (const [k, v] of Object.entries(entity.metadata.labels ?? [])) {
require(`labels.${k}`, k, this.validators.isValidLabelKey);
require(`labels.${k}`, v, this.validators.isValidLabelValue);
}
for (const [k, v] of Object.entries(entity.metadata?.annotations ?? [])) {
for (const [k, v] of Object.entries(entity.metadata.annotations ?? [])) {
require(`annotations.${k}`, k, this.validators.isValidAnnotationKey);
require(`annotations.${k}`, v, this.validators.isValidAnnotationValue);
}
@@ -50,12 +50,12 @@ export class ReservedFieldsEntityPolicy implements EntityPolicy {
`The spec may not contain the field ${field}, because it has reserved meaning`,
);
}
if (entity.metadata?.labels?.hasOwnProperty(field)) {
if (entity.metadata.labels?.hasOwnProperty(field)) {
throw new Error(
`A label may not have the field ${field}, because it has reserved meaning`,
);
}
if (entity.metadata?.annotations?.hasOwnProperty(field)) {
if (entity.metadata.annotations?.hasOwnProperty(field)) {
throw new Error(
`An annotation may not have the field ${field}, because it has reserved meaning`,
);
@@ -80,9 +80,9 @@ describe('SchemaValidEntityPolicy', () => {
// metadata
//
it('accepts missing metadata', async () => {
it('rejects missing metadata', async () => {
delete data.metadata;
await expect(policy.enforce(data)).resolves.toBe(data);
await expect(policy.enforce(data)).rejects.toThrow(/metadata/);
});
it('rejects bad metadata type', async () => {
@@ -120,9 +120,9 @@ describe('SchemaValidEntityPolicy', () => {
await expect(policy.enforce(data)).rejects.toThrow(/generation/);
});
it('accepts missing name', async () => {
it('rejects missing name', async () => {
delete data.metadata.name;
await expect(policy.enforce(data)).resolves.toBe(data);
await expect(policy.enforce(data)).rejects.toThrow(/name/);
});
it('rejects bad name type', async () => {
@@ -47,12 +47,12 @@ const DEFAULT_ENTITY_SCHEMA = yup.object({
'The generation must be an integer greater than zero',
value => value === undefined || (value === (value | 0) && value > 0),
),
name: yup.string().notRequired(),
name: yup.string().required(),
namespace: yup.string().notRequired(),
labels: yup.object<Record<string, string>>().notRequired(),
annotations: yup.object<Record<string, string>>().notRequired(),
})
.notRequired(),
.required(),
spec: yup.object({}).notRequired(),
});