MCPcopy Create free account
hub / github.com/eclipsesource/jsonforms / doCreateDefaultValue

Function doCreateDefaultValue

packages/core/src/mappers/renderer.ts:184–236  ·  view source on GitHub ↗
(
  schema: JsonSchema,
  rootSchema: JsonSchema
)

Source from the content-addressed store, hash-verified

182 * @returns the default value to use, undefined if none was found
183 */
184export 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
238const createDefaultValueForCombinatorSchema = (
239 combinatorSchemas: JsonSchema[],

Callers 2

createDefaultValueFunction · 0.85

Calls 4

hasTypeFunction · 0.90
convertDateToStringFunction · 0.90
extractDefaultsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…