import { AbstractControl , ValidatorFn } from '@angular/forms' ; import { Schema , Node as ProseMirrorNode } from 'prosemirror-model' ; import { parseContent } from './parsers' ; import defaultSchema from './schema' ; type ValidationErrors = Record < string , any > ; const isEmptyInputValue = ( value ) : boolean => { return value === null || value . length === 0 ; } ; const hasValidLength = ( value ) : boolean => { return value != null && typeof value . length === 'number' ; } ; const isDocEmpty = ( doc ) : boolean => { if ( ! doc ) { return true ; } const { childCount , firstChild } = doc ; return Boolean ( childCount === 1 && firstChild ?. isTextblock && firstChild . content . size === 0 ) ; } ; export class Validators { static required ( userSchema ? ) { return ( control ) : ValidationErrors | null => { const schema = userSchema || defaultSchema ; const doc = parseContent ( control . value , schema ) ; const isEmpty = isDocEmpty ( doc ) ; if ( ! isEmpty ) { return null ; } return { required : true } ; } ; } static maxLength ( maxLength , userSchema ? ) { return ( control ) : ValidationErrors | null => { const schema = userSchema || defaultSchema ; const doc = parseContent ( control . value , schema ) ; const value = doc . textContent ; if ( hasValidLength ( value ) && value . length > maxLength ) { return { maxlength : { requiredLength : maxLength , actualLength : value . length } } ; } return null ; } ; } static minLength ( minLength , userSchema ? ) { return ( control ) : ValidationErrors | null => { const schema = userSchema || defaultSchema ; const doc = parseContent ( control . value , schema ) ; const value = doc . textContent ; if ( isEmptyInputValue ( value ) || ! hasValidLength ( value ) ) { return null ; } if ( value . length < minLength ) { return { minlength : { requiredLength : minLength , actualLength : value . length } } ; } return null ; } ; } }