@@ -5,10 +5,15 @@ type ValidationResult = {
55 error ?: string ;
66} ;
77
8- export function validateNodeFormat (
9- format : string ,
10- nodeTypes : DiscourseNode [ ] ,
11- ) : ValidationResult {
8+ export const validateNodeFormat = ( {
9+ format,
10+ currentNode,
11+ allNodes,
12+ } : {
13+ format : string ;
14+ currentNode : DiscourseNode ;
15+ allNodes : DiscourseNode [ ] ;
16+ } ) : ValidationResult => {
1217 if ( ! format ) {
1318 return {
1419 isValid : false ,
@@ -35,13 +40,17 @@ export function validateNodeFormat(
3540 return invalidCharsResult ;
3641 }
3742
38- const uniquenessResult = validateFormatUniqueness ( nodeTypes ) ;
39- if ( ! uniquenessResult . isValid ) {
40- return uniquenessResult ;
43+ const otherNodes = allNodes . filter ( ( node ) => node . id !== currentNode . id ) ;
44+ const isDuplicate = otherNodes . some ( ( node ) => node . format === format ) ;
45+ if ( isDuplicate ) {
46+ return {
47+ isValid : false ,
48+ error : "Format must be unique across all node types" ,
49+ } ;
4150 }
4251
4352 return { isValid : true } ;
44- }
53+ } ;
4554
4655export const checkInvalidChars = ( format : string ) : ValidationResult => {
4756 const INVALID_FILENAME_CHARS_REGEX = / [ # ^ \[ \] | ] / ;
@@ -56,31 +65,21 @@ export const checkInvalidChars = (format: string): ValidationResult => {
5665 return { isValid : true } ;
5766} ;
5867
59- const validateFormatUniqueness = (
60- nodeTypes : DiscourseNode [ ] ,
61- ) : ValidationResult => {
62- const isDuplicate =
63- new Set ( nodeTypes . map ( ( nodeType ) => nodeType . format ) ) . size !==
64- nodeTypes . length ;
65-
66- if ( isDuplicate ) {
67- return { isValid : false , error : "Format must be unique" } ;
68- }
69-
70- return { isValid : true } ;
71- } ;
72-
73- export const validateNodeName = (
74- name : string ,
75- nodeTypes : DiscourseNode [ ] ,
76- ) : ValidationResult => {
68+ export const validateNodeName = ( {
69+ name,
70+ currentNode,
71+ allNodes,
72+ } : {
73+ name : string ;
74+ currentNode : DiscourseNode ;
75+ allNodes : DiscourseNode [ ] ;
76+ } ) : ValidationResult => {
7777 if ( ! name || name . trim ( ) === "" ) {
7878 return { isValid : false , error : "Name is required" } ;
7979 }
8080
81- const isDuplicate =
82- new Set ( nodeTypes . map ( ( nodeType ) => nodeType . name ) ) . size !==
83- nodeTypes . length ;
81+ const otherNodes = allNodes . filter ( ( node ) => node . id !== currentNode . id ) ;
82+ const isDuplicate = otherNodes . some ( ( node ) => node . name === name ) ;
8483
8584 if ( isDuplicate ) {
8685 return { isValid : false , error : "Name must be unique" } ;
@@ -101,14 +100,22 @@ export const validateAllNodes = (
101100 return ;
102101 }
103102
104- const formatValidation = validateNodeFormat ( nodeType . format , nodeTypes ) ;
103+ const formatValidation = validateNodeFormat ( {
104+ format : nodeType . format ,
105+ currentNode : nodeType ,
106+ allNodes : nodeTypes ,
107+ } ) ;
105108 if ( ! formatValidation . isValid ) {
106109 errorMap [ index ] = formatValidation . error || "Invalid format" ;
107110 hasErrors = true ;
108111 return ;
109112 }
110113
111- const nameValidation = validateNodeName ( nodeType . name , nodeTypes ) ;
114+ const nameValidation = validateNodeName ( {
115+ name : nodeType . name ,
116+ currentNode : nodeType ,
117+ allNodes : nodeTypes ,
118+ } ) ;
112119 if ( ! nameValidation . isValid ) {
113120 errorMap [ index ] = nameValidation . error || "Invalid name" ;
114121 hasErrors = true ;
0 commit comments