Skip to content

Commit

Permalink
fix: empty input string validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Jan 15, 2021
1 parent 26f70fa commit 9700f27
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/lib/validators.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { AbstractControl, ValidatorFn } from '@angular/forms';
import { Schema} from 'prosemirror-model';
import { Schema, Node as ProsemirrorNode } from 'prosemirror-model';

import { parseContent } from './parsers';
import defaultSchema from './schema';

type ValidationErrors = Record<string, any>;

function isEmptyInputValue(value: any): boolean {
const isEmptyInputValue = (value: any): boolean => {
// we don't check for string here so it also works with arrays
return value == null || value.length === 0;
}
};

function hasValidLength(value: any): boolean {
const hasValidLength = (value: any): boolean => {
// non-strict comparison is intentional, to check for both `null` and `undefined` values
return value != null && typeof value.length === 'number';
}
};

const isDocEmpty = (doc: ProsemirrorNode): boolean => {
if (!doc) {
return true;
}

const { childCount, firstChild } = doc;
return childCount === 1 && firstChild?.isTextblock && firstChild.content.size === 0;
};

// @dynamic
export class Validators {
Expand All @@ -26,9 +34,7 @@ export class Validators {
const schema = userSchema || defaultSchema;
const doc = parseContent(control.value, schema);

const isEmpty = doc.childCount === 1
&& doc?.firstChild?.isTextblock
&& doc.firstChild.content.size === 0;
const isEmpty = isDocEmpty(doc);

if (!isEmpty) {
return null;
Expand Down

0 comments on commit 9700f27

Please sign in to comment.