@atlaskit/adf-utils
Version:
Set of utilities to traverse, modify and create ADF documents.
150 lines (149 loc) • 4 kB
TypeScript
import type { ADFEntity, ADFEntityMark } from '../types';
export interface MarkValidationResult {
errorCode?: ValidationError['code'];
message?: string;
newMark?: ADFEntityMark;
originalMark: ADFEntityMark;
valid: boolean;
}
export interface Output {
entity?: ADFEntity;
valid: boolean;
}
export interface NodeValidationResult {
entity?: ADFEntity;
marksValidationOutput?: MarkValidationResult[];
valid: boolean;
}
export interface ValidatorContent {
allowUnsupportedBlock: boolean;
allowUnsupportedInline: boolean;
isTupleLike?: boolean;
items: Array<string | Array<string>>;
maxItems?: number;
minItems?: number;
optional?: boolean;
type: 'array';
}
export type AttributesSpec = {
maximum: number;
minimum: number;
optional?: boolean;
type: 'number';
} | {
maximum: number;
minimum: number;
optional?: boolean;
type: 'integer';
} | {
optional?: boolean;
type: 'boolean';
} | {
minLength?: number;
optional?: boolean;
pattern?: RegExp;
type: 'string';
validatorFn?: string;
} | {
optional?: boolean;
type: 'enum';
values: Array<string>;
} | {
optional?: boolean;
type: 'object';
} | {
isTupleLike?: boolean;
items: Array<AttributesSpec>;
maxItems?: number;
minItems?: number;
optional?: boolean;
type: 'array';
} | ValidatorSpecAttrs;
export interface ValidatorSpecAttrs {
optional?: boolean;
props: {
[key: string]: AttributesSpec;
};
}
export interface ValidatorSpec {
maxItems?: number;
minItems?: number;
props?: {
attrs?: ValidatorSpecAttrs;
content?: ValidatorContent;
marks?: {
items: Array<Array<string>>;
maxItems?: number;
optional?: boolean;
type: 'array';
};
text?: AttributesSpec;
type?: {
type: 'enum';
values: Array<string>;
};
};
required?: Array<string>;
}
export interface ValidationErrorMap {
DEPRECATED: never;
INVALID_ATTRIBUTES: {
attrs: Array<string>;
};
INVALID_CONTENT: {
parentType?: string;
};
INVALID_CONTENT_LENGTH: {
length: number;
requiredLength: number;
type: RequiredContentLength;
};
INVALID_TEXT: never;
INVALID_TYPE: never;
MISSING_PROPERTIES: {
props: Array<string>;
};
REDUNDANT_ATTRIBUTES: {
attrs: Array<string>;
};
REDUNDANT_MARKS: {
marks: Array<string>;
};
REDUNDANT_PROPERTIES: {
props: Array<string>;
};
UNSUPPORTED_ATTRIBUTES: {
attrs: Array<string>;
};
}
export type RequiredContentLength = 'minimum' | 'maximum';
export type Content = Array<string | [
string,
object
] | Array<string>>;
export type ValidationErrorType = keyof ValidationErrorMap;
export interface ValidationError {
code: ValidationErrorType;
message: string;
meta?: object;
}
export type ErrorCallback = (entity: ADFEntity, error: ValidationError, options: ErrorCallbackOptions) => ADFEntity | undefined;
export type ValidationMode = 'strict' | 'loose';
export interface ValidationOptions {
allowPrivateAttributes?: boolean;
mode?: ValidationMode;
}
export interface SpecValidatorResult {
hasValidated: boolean;
result?: NodeValidationResult;
}
export type Err = <T extends ValidationErrorType>(code: T, msg: string, meta?: T extends keyof ValidationErrorMap ? ValidationErrorMap[T] : never) => NodeValidationResult;
export interface ErrorCallbackOptions {
allowNestedTables?: boolean;
allowUnsupportedBlock?: boolean;
allowUnsupportedInline?: boolean;
isMark?: any;
isNodeAttribute?: boolean;
}
export type Validate = (entity: ADFEntity, errorCallback?: ErrorCallback, allowed?: Content, parentSpec?: ValidatorSpec) => Output;
export type CreateSpecReturn = Record<string, ValidatorSpec | Record<string, ValidatorSpec | string>>;