mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 08:33:11 +00:00
feat: generator updates
This commit is contained in:
62
packages/core-validator/src/index.js
Normal file
62
packages/core-validator/src/index.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import Ajv from 'ajv';
|
||||
import addFormats from 'ajv-formats';
|
||||
import schema from '@openstapps/core/schema.json' assert {type: 'json'};
|
||||
|
||||
/**
|
||||
* StAppsCore validator
|
||||
* @typedef {import('ajv').AnySchema} AnySchema
|
||||
* @typedef {import('@openstapps/core/schema.json').SchemaMap} SchemaMap
|
||||
*/
|
||||
export class Validator {
|
||||
/**
|
||||
* @private
|
||||
* @readonly
|
||||
* @type {Ajv.default}
|
||||
*/
|
||||
ajv;
|
||||
|
||||
get errors() {
|
||||
return this.ajv.errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param additionalSchemas {AnySchema[]}
|
||||
*/
|
||||
constructor(additionalSchemas = []) {
|
||||
this.ajv = new Ajv.default({
|
||||
schemas: [schema, ...additionalSchemas],
|
||||
verbose: true,
|
||||
keywords: ['elasticsearch'],
|
||||
allowUnionTypes: true,
|
||||
});
|
||||
addFormats.default(this.ajv, {
|
||||
formats: ['date-time', 'time', 'uuid', 'duration'],
|
||||
mode: 'fast',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add additional schemas to the validator
|
||||
* @param schema {AnySchema[]}
|
||||
*/
|
||||
addSchema(...schema) {
|
||||
this.ajv.addSchema(schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates anything against a given schema name or infers schema name from object
|
||||
* @template {SchemaMap[keyof SchemaMap]} T
|
||||
*
|
||||
* @param instance {unknown} Instance to validate
|
||||
* @param schema {import('./types.js').NameOf<T>} Name of schema to validate instance against or the schema itself
|
||||
* @returns {instance is T}
|
||||
*
|
||||
*/ /**
|
||||
* @param instance {unknown}
|
||||
* @param schema {Ajv.Schema | string}
|
||||
* @returns {boolean}
|
||||
*/
|
||||
validate(instance, schema) {
|
||||
return this.ajv.validate(typeof schema === 'string' ? `#/definitions/${schema}` : schema, instance);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
import Ajv, {AnySchema} from 'ajv';
|
||||
import addFormats from 'ajv-formats';
|
||||
import schema from '@openstapps/core/schema.json' assert {type: 'json'};
|
||||
import type {SchemaMap} from '@openstapps/core/schema.json';
|
||||
|
||||
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;
|
||||
|
||||
get errors() {
|
||||
return this.ajv.errors;
|
||||
}
|
||||
|
||||
constructor(additionalSchemas: AnySchema[] = []) {
|
||||
this.ajv = new Ajv.default({
|
||||
schemas: [schema, ...additionalSchemas],
|
||||
verbose: true,
|
||||
keywords: ['elasticsearch'],
|
||||
allowUnionTypes: true,
|
||||
});
|
||||
addFormats.default(this.ajv, {
|
||||
formats: ['date-time', 'time', 'uuid', 'duration'],
|
||||
mode: 'fast',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add additional schemas to the validator
|
||||
*/
|
||||
public addSchema(...schema: AnySchema[]) {
|
||||
this.ajv.addSchema(schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 extends SchemaMap[keyof SchemaMap]>(instance: unknown, schema: NameOf<T>): instance is T;
|
||||
public validate(instance: unknown, schema: Ajv.Schema): boolean;
|
||||
public validate(instance: unknown, schema: string): boolean;
|
||||
public validate(instance: unknown, schema: Ajv.Schema | string): boolean {
|
||||
return this.ajv.validate(typeof schema === 'string' ? `#/definitions/${schema}` : schema, instance);
|
||||
}
|
||||
}
|
||||
19
packages/core-validator/src/types.d.ts
vendored
Normal file
19
packages/core-validator/src/types.d.ts
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
import {SchemaMap} from '@openstapps/core/schema.json';
|
||||
|
||||
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;
|
||||
}>;
|
||||
|
||||
export type NameOf<I extends SchemaMap[keyof SchemaMap]> = keyof IncludeProperty<SchemaMap, I>;
|
||||
|
||||
export {Validator} from './index.js';
|
||||
Reference in New Issue
Block a user