Interface IPromptJsonSchemaService


  • public interface IPromptJsonSchemaService
    Service for prompt service providers that provides functionality related to JSON schemas. Usage of this service is optional, but may simplify implementation of prompt service handlers. Obtain instances via CDI.
    Since:
    8.5.0
    • Method Detail

      • constructJsonSchemaFromPropertyList

        String constructJsonSchemaFromPropertyList​(JsonSchemaOutputByPropertyList propertyList)
        Creates a JSON schema from a given list of properties. In particular, creates a JSON schema for an object with the given properties that does not allow additional properties. The type of each property is determined by the type of the property in the given list. If present, the description is also added from the property to the schema.
        Parameters:
        propertyList - The list of properties to create the JSON schema from.
        Returns:
        A JSON schema as a string.
      • derivePropertyListFromSchema

        JsonSchemaOutputByPropertyList derivePropertyListFromSchema​(Reader reader)
                                                             throws JsonSchemaServiceException
        Tries to convert a given JSON schema to a list of properties. This will usually result in a loss of information, as not all JSON schema constructs can be represented in a list of properties. Only top level properties of an object are included in the result. If the schema is a primitive type (null, boolean, number, string), returns a list with a single property named "value" of the corresponding type.
        Parameters:
        reader - The reader to read the JSON schema from.
        Returns:
        The list of properties.
        Throws:
        JsonSchemaServiceException - When the reader could not be read, the given JSON schema is invalid, or when the schema could not be converted for some reason.
      • deriveSuperTypeValueDescriptorFromJsonSchema

        IValueDescriptor<?,​?> deriveSuperTypeValueDescriptorFromJsonSchema​(Reader reader)
                                                                          throws JsonSchemaServiceException
        Derives a IValueDescriptor from a given JSON schema. Value descriptors are (intentionally) less expressive than JSON schemas, so no perfect mapping is possible. This method returns a value descriptor that is a super type of the given JSON schema. More precisely, given a JSON schema J, returns a value descriptor V such that for every value v that matches J, v also matches V. The inverse does not hold: There may be values that match V but not J.

        Put differently, if a value is valid against the given JSON schema, then it is also valid against the returned value descriptor, but not vice versa. It would be valid for this method to always return an any value descriptor regardless of the input, but implementations should try to be more specific where possible.

        Parameters:
        reader - The reader to read the JSON schema from.
        Returns:
        The derived value descriptor.
        Throws:
        JsonSchemaServiceException - When the reader could not be read, the given JSON is invalid, or when the value descriptor could not be derived for some reason.
      • inferSchemaFromJson

        String inferSchemaFromJson​(Reader reader)
                            throws JsonSchemaServiceException
        Given a sample JSON (which can be an object, array, string, number, boolean or null), infers a JSON schema for that JSON. There are no guarantees what the schema will look like, except that (a) it will be a valid JSON schema according to the JSON Schema specification Draft 2020/12 and (b) the given JSON will be valid against the returned schema.
        Parameters:
        reader - The reader to read the JSON from.
        Returns:
        A JSON schema as a string.
        Throws:
        JsonSchemaServiceException - When the reader could not be read, the given JSON is invalid, or when the schema could not be inferred for some reason.
      • transformJsonSchema

        IJsonSchemaTransformerBuilder transformJsonSchema()
        Gets a builder for transforming JSON schemas. The builder lets you add various transform steps to apply to the schema. Once you've configured the transform, call the IJsonSchemaTransformerBuilder.build() to obtain the transformer instance, then call transform to transform a JSON schema.
        Returns:
        A builder for transforming JSON schemas.
      • validateJson

        IJsonValidationResult validateJson​(Reader reader)
        Validates whether a given string represents a valid JSON value.
        Parameters:
        reader - The reader to read the JSON from.
        Returns:
        The result of the validation.
      • validateJsonSchema

        IJsonValidationResult validateJsonSchema​(Reader reader)
        Validates whether the given JSON text is a valid JSON schema, according to the JSON Schema specification Draft 2020/12. Never throws an exception. When the data could not be read from the reader, a IJsonValidationResult with an appropriate error message is returned.
        Parameters:
        reader - The reader to read the JSON schema from.
        Returns:
        The result of the validation.