Skip to main content
Version: 6.0

validateRequired

function validateRequired(processedEntity: any, requiredDefaults: Record<string, unknown>): string | undefined;

Returns a string message if any keys of requiredDefaults are missing in processedEntity. This can be used to validate fields that must be provided.

class CustomBaseEntity extends Entity {
static validate(processedEntity) {
return validateRequired(processedEntity, this.defaults) || super.validate(processedEntity);
}
}

Partial/full results

This can be useful to automatically validate for partial results

class SummaryAnalysis extends Entity {
readonly id: string = '';
readonly createdAt: Date = new Date(0);
readonly meanValue: number = 0;
readonly title: string = '';

pk() {
return this.id;
}
}

class FullAnalysis extends SummaryAnalysis {
readonly graph: number[] = [];
}

Optional fields

In case we have a field that won't always be present (like lastRun here), we can simply 'exclude' it from the fields we require.

class FullAnalysis extends SummaryAnalysis {
readonly graph: number[] = [];
readonly lastRun?: Date = new Date(0);

static schema = {
lastRun: Date,
}

static validate(processedEntity) {
return validateRequired(processedEntity, exclude(this.defaults, ['lastRun']));
}
}
exclude()
exclude
function exclude<O extends Record<string, unknown>>(
obj: O,
keys: string[],
): Partial<O> {
const r: any = {};
Object.keys(obj).forEach(k => {
if (!keys.includes(k)) r[k] = obj[k];
});
return r;
}

Full results only have optional fields

In case every field of the 'full' resource was optional:

class FullAnalysis extends SummaryAnalysis {
readonly graph?: number[] = [];
readonly lastRun?: Date = new Date(0);

static schema = {
lastRun: Date,
}

static validate(processedEntity) {
return validateRequired(processedEntity, exclude(this.defaults, ['graph', 'lastRun']));
}
}

This code would not successfully know to fetch the 'full' resource if the summary is already provided. There would be no way of knowing whether the fields simply don't exist for that data, or were not fetched. In this case, it is best to provide a null default for at least one field.

class FullAnalysis extends SummaryAnalysis {
readonly graph: number[] = null;
readonly lastRun?: Date = new Date(0);

static schema = {
lastRun: Date,
}

static validate(processedEntity) {
return validateRequired(processedEntity, exclude(this.defaults, ['lastRun']));
}
}

This enables the client to understand whether the 'full' resource has been fetched at all.