snapshot of https://github.com/dream-num/univer.git (dev, 2026-09-16; blobs > 131072B omitted)
Some checks failed
📦 Build / build (push) Has been cancelled
🧪 CI Test / test shard (1/4) (push) Has been cancelled
🧪 CI Test / test shard (2/4) (push) Has been cancelled
🧪 CI Test / test shard (3/4) (push) Has been cancelled
🧪 CI Test / test shard (4/4) (push) Has been cancelled
🧪 CI Test / test (push) Has been cancelled
🧪 CI Test / eslint (push) Has been cancelled
🧪 CI Test / typecheck (push) Has been cancelled
🔫 Sync to Pro / dispatch-sync (push) Has been cancelled
🔮 Sync Mirror / sync (push) Has been cancelled

This commit is contained in:
upstream-mirror
2026-09-16 05:09:45 +05:00
commit bcf6ef63ad
7118 changed files with 1236854 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Injector } from '@univerjs/core';
import { beforeEach, describe, expect, it } from 'vitest';
import { DataValidatorRegistryScope, DataValidatorRegistryService } from '../data-validator-registry.service';
describe('DataValidatorRegistryService', () => {
let service: DataValidatorRegistryService;
beforeEach(() => {
const injector = new Injector();
injector.add([DataValidatorRegistryService]);
service = injector.get(DataValidatorRegistryService);
});
it('registers validators by business scope and unregisters them with the disposable', () => {
const changes: number[] = [];
service.validatorsChange$.subscribe(() => changes.push(1));
const validator = { id: 'decimal', scopes: DataValidatorRegistryScope.SHEET };
const disposable = service.register(validator as never);
expect(service.getValidatorItem('decimal')).toBe(validator);
expect(service.getValidatorsByScope(DataValidatorRegistryScope.SHEET)).toEqual([validator]);
disposable.dispose();
expect(service.getValidatorItem('decimal')).toBeUndefined();
expect(service.getValidatorsByScope(DataValidatorRegistryScope.SHEET)).toEqual([]);
expect(changes.length).toBe(3);
});
it('rejects duplicate validators in the same scope', () => {
service.register({ id: 'decimal', scopes: DataValidatorRegistryScope.SHEET } as never);
expect(() => service.register({ id: 'decimal', scopes: DataValidatorRegistryScope.SHEET } as never)).toThrow(
'Validator item with the same id decimal has already been added!'
);
});
});

View File

@@ -0,0 +1,95 @@
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { BaseDataValidator } from '../validators/base-data-validator';
import { toDisposable } from '@univerjs/core';
import { BehaviorSubject } from 'rxjs';
export enum DataValidatorRegistryScope {
SHEET = 'sheet',
}
/**
* Register data validator
*/
export class DataValidatorRegistryService {
private _validatorByScopes = new Map<string, Array<BaseDataValidator>>();
private _validatorMap = new Map<string, BaseDataValidator>();
private _validatorsChange$ = new BehaviorSubject<void>(undefined);
validatorsChange$ = this._validatorsChange$.asObservable();
private _addValidatorToScope(validator: BaseDataValidator, scope: string) {
if (!this._validatorByScopes.has(scope)) {
this._validatorByScopes.set(scope, []);
}
const validators = this._validatorByScopes.get(scope)!;
if (validators.findIndex((m) => m.id === validator.id) > -1) {
throw new Error(`Validator item with the same id ${validator.id} has already been added!`);
}
validators.push(validator);
}
private _removeValidatorFromScope(validator: BaseDataValidator, scope: string) {
const validators = this._validatorByScopes.get(scope);
if (!validators) {
return;
}
const index = validators.findIndex((v) => v.id === validator.id);
if (index > -1) {
validators.splice(index, 1);
}
}
register(validator: BaseDataValidator) {
this._validatorMap.set(validator.id, validator);
if (Array.isArray(validator.scopes)) {
validator.scopes.forEach((scope) => {
this._addValidatorToScope(validator, scope);
});
} else {
this._addValidatorToScope(validator, validator.scopes);
}
this._validatorsChange$.next();
return toDisposable(() => {
this._validatorMap.delete(validator.id);
if (Array.isArray(validator.scopes)) {
validator.scopes.forEach((scope) => {
this._removeValidatorFromScope(validator, scope);
});
} else {
this._removeValidatorFromScope(validator, validator.scopes);
}
this._validatorsChange$.next();
});
}
getValidatorItem(id: string) {
return this._validatorMap.get(id);
}
getValidatorsByScope(scope: string) {
return this._validatorByScopes.get(scope);
}
}