( schema: JsonSchema, rootSchema: JsonSchema )
| 182 | * @returns the default value to use, undefined if none was found |
| 183 | */ |
| 184 | export const doCreateDefaultValue = ( |
| 185 | schema: JsonSchema, |
| 186 | rootSchema: JsonSchema |
| 187 | ) => { |
| 188 | const resolvedSchema = |
| 189 | typeof schema.$ref === 'string' |
| 190 | ? Resolve.schema(rootSchema, schema.$ref, rootSchema) |
| 191 | : schema; |
| 192 | if (resolvedSchema.default !== undefined) { |
| 193 | return extractDefaults(resolvedSchema, rootSchema); |
| 194 | } |
| 195 | if (hasType(resolvedSchema, 'string')) { |
| 196 | if ( |
| 197 | resolvedSchema.format === 'date-time' || |
| 198 | resolvedSchema.format === 'date' || |
| 199 | resolvedSchema.format === 'time' |
| 200 | ) { |
| 201 | return convertDateToString(new Date(), resolvedSchema.format); |
| 202 | } |
| 203 | return ''; |
| 204 | } |
| 205 | if (hasType(resolvedSchema, 'integer') || hasType(resolvedSchema, 'number')) { |
| 206 | return 0; |
| 207 | } |
| 208 | if (hasType(resolvedSchema, 'boolean')) { |
| 209 | return false; |
| 210 | } |
| 211 | if (hasType(resolvedSchema, 'array')) { |
| 212 | return []; |
| 213 | } |
| 214 | if (hasType(resolvedSchema, 'object')) { |
| 215 | return extractDefaults(resolvedSchema, rootSchema); |
| 216 | } |
| 217 | if (hasType(resolvedSchema, 'null')) { |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | const combinators: CombinatorKeyword[] = ['oneOf', 'anyOf', 'allOf']; |
| 222 | for (const combinator of combinators) { |
| 223 | if (schema[combinator] && Array.isArray(schema[combinator])) { |
| 224 | const combinatorDefault = createDefaultValueForCombinatorSchema( |
| 225 | schema[combinator], |
| 226 | rootSchema |
| 227 | ); |
| 228 | if (combinatorDefault !== undefined) { |
| 229 | return combinatorDefault; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // no default value found |
| 235 | return undefined; |
| 236 | }; |
| 237 | |
| 238 | const createDefaultValueForCombinatorSchema = ( |
| 239 | combinatorSchemas: JsonSchema[], |
no test coverage detected
searching dependent graphs…