@@ -37,26 +37,32 @@ const numberValidationFormatters: Record<string, (value: unknown) => string> = {
3737 maxLength : value => `<= ${ value } characters` ,
3838} ;
3939
40- const validationsFormatter = ( name : string ) => ( value : unknown [ ] | unknown ) : ValidationFormat | null => {
40+ const createStringFormatter = ( nowrap : boolean | undefined ) => ( value : unknown ) => {
41+ return nowrap || typeof value !== 'string' ? String ( value ) : `"${ value } "` ;
42+ } ;
43+
44+ const createValidationsFormatter = ( name : string , options ?: { exact ?: boolean ; nowrap ?: boolean } ) => (
45+ value : unknown [ ] | unknown ,
46+ ) : ValidationFormat | null => {
4147 const values = Array . isArray ( value ) ? value : [ value ] ;
4248 if ( values . length ) {
4349 return {
44- name : values . length > 1 ? `${ name } values` : `${ name } value` ,
45- values : values . map ( stringifyValue ) ,
50+ name : options ?. exact ? name : values . length > 1 ? `${ name } values` : `${ name } value` ,
51+ values : values . map ( createStringFormatter ( options ?. nowrap ) ) ,
4652 } ;
4753 }
4854 return null ;
4955} ;
5056
5157const validationFormatters : Record < string , ( value : unknown ) => ValidationFormat | null > = {
52- [ 'const' ] : validationsFormatter ( 'Allowed' ) ,
53- enum : validationsFormatter ( 'Allowed' ) ,
54- examples : validationsFormatter ( 'Example' ) ,
55- example : validationsFormatter ( 'Example' ) ,
56- [ 'x-example' ] : validationsFormatter ( 'Example' ) ,
57- multipleOf : validationsFormatter ( 'Multiple of' ) ,
58- pattern : validationsFormatter ( 'Match pattern' ) ,
59- default : validationsFormatter ( 'Default' ) ,
58+ [ 'const' ] : createValidationsFormatter ( 'Allowed' ) ,
59+ enum : createValidationsFormatter ( 'Allowed' ) ,
60+ examples : createValidationsFormatter ( 'Example' ) ,
61+ example : createValidationsFormatter ( 'Example' ) ,
62+ [ 'x-example' ] : createValidationsFormatter ( 'Example' ) ,
63+ multipleOf : createValidationsFormatter ( 'Multiple of' , { exact : true } ) ,
64+ pattern : createValidationsFormatter ( 'Match pattern' , { exact : true , nowrap : true } ) ,
65+ default : createValidationsFormatter ( 'Default' ) ,
6066} ;
6167
6268export const Validations : React . FunctionComponent < IValidations > = ( { validations } ) => {
@@ -125,7 +131,7 @@ const KeyValueValidation = ({ className, name, values }: { className?: string; n
125131 return (
126132 < Flex flexWrap color = "muted" my = { 2 } className = { className } >
127133 < Text fontWeight = "light" > { capitalize ( name ) } :</ Text >
128- { values . map ( value => (
134+ { uniq ( values ) . map ( value => (
129135 < Text key = { value } ml = { 2 } px = { 1 } fontFamily = "mono" border rounded = "lg" className = { className } >
130136 { value }
131137 </ Text >
@@ -178,7 +184,3 @@ export function getValidationsFromSchema(schemaNode: RegularNode) {
178184 ...schemaNode . validations ,
179185 } ;
180186}
181-
182- function stringifyValue ( value : unknown ) {
183- return typeof value === 'string' ? `"${ value } "` : String ( value ) ;
184- }
0 commit comments