dist/@ajsf/core/esm2015/lib/shared/json-schema.functions.js
/**
 * 'buildSchemaFromData' function
 *
 * Build a JSON Schema from a data object
 *
 * //   data - The data object
 * //  { boolean = false } requireAllFields - Require all fields?
 * //  { boolean = true } isRoot - is root
 * //  - The new JSON Schema
 */
'buildSchemaFromData' function
Build a JSON Schema from a data object

The data object
{ boolean = false } requireAllFields - Require all fields.
{ boolean = true } isRoot - is root
- The new JSON Schema
' buildSchemaFromData ' function Build a JSON Schema from a data object The data object { boolean = false } requireAllFields - Require all fields . { boolean = true } isRoot - is root - The new JSON Schema
function buildSchemaFromData(data, requireAllFields = false, isRoot = true) {
    const newSchema = {};
    const getFieldType = (value) => {
        const fieldType = getType(value, 'strict');
        return { integer: 'number', null: 'string' }[fieldType] || fieldType;
    };
    const buildSubSchema = (value) => buildSchemaFromData(value, requireAllFields, false);
    if (isRoot) {
        newSchema.$schema = 'http://json-schema.org/draft-06/schema#';
    }
    newSchema.type = getFieldType(data);
    if (newSchema.type === 'object') {
        newSchema.properties = {};
        if (requireAllFields) {
            newSchema.required = [];
        }
        for (const key of Object.keys(data)) {
            newSchema.properties[key] = buildSubSchema(data[key]);
            if (requireAllFields) {
                newSchema.required.push(key);
            }
        }
    }
    else if (newSchema.type === 'array') {
        newSchema.items = data.map(buildSubSchema);
        if ((new Set(data.map(getFieldType))).size === 1) {
            newSchema.items = newSchema.items.reduce((a, b) => (Object.assign(Object.assign({}, a), b)), {});
        }
        if (requireAllFields) {
            newSchema.minItems = 1;
        }
    }
    return newSchema;
}
function buildSchemaFromData ( data , requireAllFields = false , isRoot = true ) { const newSchema = { } ; const getFieldType = ( value ) => { const fieldType = getType ( value , 'strict' ) ; return { integer : 'number' , null : 'string' } [ fieldType ] || fieldType ; } ; const buildSubSchema = ( value ) => buildSchemaFromData ( value , requireAllFields , false ) ; if ( isRoot ) { newSchema . $schema = 'http://json-schema.org/draft-06/schema#' ; } newSchema . type = getFieldType ( data ) ; if ( newSchema . type === 'object' ) { newSchema . properties = { } ; if ( requireAllFields ) { newSchema . required = [ ] ; } for ( const key of Object . keys ( data ) ) { newSchema . properties [ key ] = buildSubSchema ( data [ key ] ) ; if ( requireAllFields ) { newSchema . required . push ( key ) ; } } } else if ( newSchema . type === 'array' ) { newSchema . items = data . map ( buildSubSchema ) ; if ( ( new Set ( data . map ( getFieldType ) ) ) . size === 1 ) { newSchema . items = newSchema . items . reduce ( ( a , b ) => ( Object . assign ( Object . assign ( { } , a ) , b ) ) , { } ) ; } if ( requireAllFields ) { newSchema . minItems = 1 ; } } return newSchema ; }
'buildSchemaFromData' function
 Build a JSON Schema from a data object
' buildSchemaFromData ' function Build a JSON Schema from a data object
// If all items are the same type, use an object for items instead of an array