How to use the @loopback/repository.resolveType function in @loopback/repository

To help you get started, we’ve selected a few @loopback/repository examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / packages / repository-json-schema / src / build-schema.ts View on Github external
// populating "properties" key
    result.properties[p] = metaToJsonProperty(metaProperty);

    // handling 'required' metadata
    const optional = options.optional.includes(p as keyof T);

    if (metaProperty.required && !(partial || optional)) {
      result.required = result.required ?? [];
      result.required.push(p);
    }

    // populating JSON Schema 'definitions'
    // shimks: ugly type casting; this should be replaced by logic to throw
    // error if itemType/type is not a string or a function
    const resolvedType = resolveType(metaProperty.type) as string | Function;
    const referenceType = isArrayType(resolvedType)
      ? // shimks: ugly type casting; this should be replaced by logic to throw
        // error if itemType/type is not a string or a function
        resolveType(metaProperty.itemType as string | Function)
      : resolvedType;

    if (typeof referenceType !== 'function' || isBuiltinType(referenceType)) {
      continue;
    }

    const propSchema = getJsonSchema(referenceType, options);

    includeReferencedSchema(referenceType.name, propSchema);
  }

  result.additionalProperties = meta.settings.strict === false;
github strongloop / loopback-next / packages / repository-json-schema / src / build-schema.ts View on Github external
continue;
    }

    const propSchema = getJsonSchema(referenceType, options);

    includeReferencedSchema(referenceType.name, propSchema);
  }

  result.additionalProperties = meta.settings.strict === false;
  debug('  additionalProperties?', result.additionalProperties);

  if (options.includeRelations) {
    for (const r in meta.relations) {
      result.properties = result.properties ?? {};
      const relMeta = meta.relations[r];
      const targetType = resolveType(relMeta.target);
      const targetSchema = getJsonSchema(targetType, options);
      const targetRef = {$ref: `#/definitions/${targetSchema.title}`};
      const propDef = getNavigationalPropertyForRelation(relMeta, targetRef);

      result.properties[relMeta.name] =
        result.properties[relMeta.name] || propDef;
      includeReferencedSchema(targetSchema.title!, targetSchema);
    }
  }

  function includeReferencedSchema(name: string, schema: JSONSchema) {
    if (!schema || !Object.keys(schema).length) return;

    // promote nested definition to the top level
    if (result !== schema && schema.definitions) {
      for (const key in schema.definitions) {
github strongloop / loopback-next / packages / repository-json-schema / src / build-schema.ts View on Github external
// handling 'required' metadata
    const optional = options.optional.includes(p as keyof T);

    if (metaProperty.required && !(partial || optional)) {
      result.required = result.required ?? [];
      result.required.push(p);
    }

    // populating JSON Schema 'definitions'
    // shimks: ugly type casting; this should be replaced by logic to throw
    // error if itemType/type is not a string or a function
    const resolvedType = resolveType(metaProperty.type) as string | Function;
    const referenceType = isArrayType(resolvedType)
      ? // shimks: ugly type casting; this should be replaced by logic to throw
        // error if itemType/type is not a string or a function
        resolveType(metaProperty.itemType as string | Function)
      : resolvedType;

    if (typeof referenceType !== 'function' || isBuiltinType(referenceType)) {
      continue;
    }

    const propSchema = getJsonSchema(referenceType, options);

    includeReferencedSchema(referenceType.name, propSchema);
  }

  result.additionalProperties = meta.settings.strict === false;
  debug('  additionalProperties?', result.additionalProperties);

  if (options.includeRelations) {
    for (const r in meta.relations) {
github strongloop / loopback-next / packages / repository-json-schema / src / build-schema.ts View on Github external
const propDef: JSONSchema = {};
  let result: JSONSchema;
  let propertyType = meta.type as string | Function;

  if (isArrayType(propertyType) && meta.itemType) {
    if (Array.isArray(meta.itemType)) {
      throw new Error('itemType as an array is not supported');
    }
    result = {type: 'array', items: propDef};
    propertyType = meta.itemType as string | Function;
  } else {
    result = propDef;
  }

  const wrappedType = stringTypeToWrapper(propertyType);
  const resolvedType = resolveType(wrappedType);

  if (resolvedType === Date) {
    Object.assign(propDef, {
      type: 'string',
      format: 'date-time',
    });
  } else if (isBuiltinType(resolvedType)) {
    Object.assign(propDef, {
      type: resolvedType.name.toLowerCase(),
    });
  } else {
    Object.assign(propDef, {$ref: `#/definitions/${resolvedType.name}`});
  }

  if (meta.description) {
    Object.assign(propDef, {