import { abortOnNonSchema } from '~/data/helpers/is-themis-schema/load/abort-on-non-schema'; import { CheckSchemaKeyAdapter } from '~/data/helpers/is-themis-schema/load/check-key'; import { IIsThemisSchema } from '~/data/helpers/is-themis-schema/protocol'; import { schemaTypes, ValidationSchemas } from '~/domain'; import { BaseSchema } from '~/domain/entities/schemas/any-schema'; export class IsThemisSchemaAdapter implements IIsThemisSchema { private readonly checkKey: CheckSchemaKeyAdapter; constructor(private readonly abort: boolean) { this.checkKey = new CheckSchemaKeyAdapter({ rules: value => typeof value === 'object', type: value => schemaTypes.includes(value), validate: value => typeof value === 'function', }); } isSchema = (value: unknown): value is ValidationSchemas => { if (this.abort) { if (value && typeof value === 'object') { return [ () => this.checkKey.check(value, 'type'), () => this.checkKey.check(value, 'rules'), () => this.checkKey.check(value, 'validate'), () => { const instanceOfSchema = value instanceof BaseSchema; if (!instanceOfSchema) { abortOnNonSchema(value, this.abort); } return instanceOfSchema; }, ].every(checker => checker()); } return false; } return value instanceof BaseSchema; }; }