@@ -15,7 +15,7 @@ function getMeta({ $comment: comment, title, description }) {
1515}
1616
1717// TODO provide types
18- function traverse ( schema , path , resolve , rootSchema ) {
18+ function traverse ( schema , path , resolve , rootSchema , validateSchema ) {
1919 schema = resolve ( schema , null , path ) ;
2020
2121 if ( schema && ( schema . oneOf || schema . anyOf || schema . allOf ) ) {
@@ -36,14 +36,42 @@ function traverse(schema, path, resolve, rootSchema) {
3636 // example values have highest precedence
3737 if ( optionAPI ( 'useExamplesValue' ) && Array . isArray ( schema . examples ) ) {
3838 // include `default` value as example too
39- const fixedExamples = schema . examples
40- . concat ( 'default' in schema ? [ schema . default ] : [ ] ) ;
41-
42- return { value : utils . typecast ( null , schema , ( ) => random . pick ( fixedExamples ) ) , context } ;
39+ const fixedExamples = schema . examples . concat (
40+ 'default' in schema ? [ schema . default ] : [ ] ) ;
41+ const randomExample = random . pick ( fixedExamples ) ;
42+ if ( validateSchema ) {
43+ const result = validateSchema ( schema , randomExample ) ;
44+ // Use example only if valid
45+ if ( result && result . length === 0 ) {
46+ return {
47+ value : utils . typecast ( null , schema , ( ) => randomExample ) ,
48+ context,
49+ } ;
50+ }
51+ } else {
52+ console . warn ( 'Taking example from schema without validation' ) ;
53+ return {
54+ value : utils . typecast ( null , schema , ( ) => randomExample ) ,
55+ context,
56+ } ;
57+ }
4358 }
4459 // If schema contains single example property
4560 if ( optionAPI ( 'useExamplesValue' ) && schema . example ) {
46- return { value : utils . typecast ( null , schema , ( ) => schema . example ) , context } ;
61+ if ( validateSchema ) {
62+ const result = validateSchema ( schema , schema . example ) ;
63+
64+ // Use example only if valid
65+ if ( result && result . length === 0 ) {
66+ return {
67+ value : utils . typecast ( null , schema , ( ) => schema . example ) ,
68+ context,
69+ } ;
70+ }
71+ } else {
72+ console . warn ( 'Taking example from schema without validation' ) ;
73+ return { value : utils . typecast ( null , schema , ( ) => schema . example ) , context } ;
74+ }
4775 }
4876
4977 if ( optionAPI ( 'useDefaultValue' ) && 'default' in schema ) {
@@ -66,15 +94,15 @@ function traverse(schema, path, resolve, rootSchema) {
6694
6795 // build new object value from not-schema!
6896 if ( schema . type && schema . type === 'object' ) {
69- const { value, context : innerContext } = traverse ( schema , path . concat ( [ 'not' ] ) , resolve , rootSchema ) ;
97+ const { value, context : innerContext } = traverse ( schema , path . concat ( [ 'not' ] ) , resolve , rootSchema , validateSchema ) ;
7098 return { value : utils . clean ( value , schema , false ) , context : { ...context , items : innerContext } } ;
7199 }
72100 }
73101
74102 // thunks can return sub-schemas
75103 if ( typeof schema . thunk === 'function' ) {
76104 // result is already cleaned in thunk
77- const { value, context : innerContext } = traverse ( schema . thunk ( rootSchema ) , path , resolve ) ;
105+ const { value, context : innerContext } = traverse ( schema . thunk ( rootSchema ) , path , resolve , undefined , validateSchema ) ;
78106 return { value, context : { ...context , items : innerContext } } ;
79107 }
80108
@@ -172,7 +200,7 @@ function traverse(schema, path, resolve, rootSchema) {
172200 Object . keys ( schema ) . forEach ( prop => {
173201 if ( pruneProperties . includes ( prop ) ) return ;
174202 if ( typeof schema [ prop ] === 'object' && prop !== 'definitions' ) {
175- const { value, context : innerContext } = traverse ( schema [ prop ] , path . concat ( [ prop ] ) , resolve , valueCopy ) ;
203+ const { value, context : innerContext } = traverse ( schema [ prop ] , path . concat ( [ prop ] ) , resolve , valueCopy , validateSchema ) ;
176204 valueCopy [ prop ] = utils . clean ( value , schema [ prop ] , false ) ;
177205 contextCopy [ prop ] = innerContext ;
178206 } else {
0 commit comments