feat: improve monorepo dev experience

This commit is contained in:
2023-10-27 22:45:44 +02:00
parent f618725598
commit c6ab4ae48b
124 changed files with 2647 additions and 2857 deletions

View File

@@ -0,0 +1,48 @@
import Ajv, {AnySchema} from 'ajv';
import addFormats from 'ajv-formats';
import schema from '../schema/core.schema.json';
import {SchemaMap} from '../schema/core.schema.js';
export type RemoveNeverProperties<T> = {
[K in Exclude<
keyof T,
{
// eslint-disable-next-line @typescript-eslint/ban-types
[P in keyof T]: T[P] extends Function ? P : never;
}[keyof T]
>]: T[K];
};
export type IncludeProperty<T extends object, E> = RemoveNeverProperties<{
[K in keyof T]: T[K] extends E ? T[K] : never;
}>;
type NameOf<I extends SchemaMap[keyof SchemaMap]> = keyof IncludeProperty<SchemaMap, I>;
/**
* StAppsCore validator
*/
export class Validator {
private readonly ajv: Ajv.default;
constructor(additionalSchemas: AnySchema[] = []) {
this.ajv = new Ajv.default({
schemas: [schema, ...additionalSchemas],
verbose: true,
allowUnionTypes: true,
});
addFormats.default(this.ajv, {
formats: ['date-time', 'time', 'uuid', 'duration'],
mode: 'fast',
});
}
/**
* Validates anything against a given schema name or infers schema name from object
* @param instance Instance to validate
* @param schema Name of schema to validate instance against or the schema itself
*/
public validate<T>(instance: unknown, schema: NameOf<T>): instance is T {
return this.ajv.validate(schema as string, instance);
}
}