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 Summary
Modifier and TypeMethodDescriptionCreates a JSON schema from a given list of properties.derivePropertyListFromSchema(Reader reader) Tries to convert a given JSON schema to a list of properties.IValueDescriptor<?, ?> Derives aIValueDescriptorfrom a given JSON schema.inferSchemaFromJson(Reader reader) Given a sample JSON (which can be an object, array, string, number, boolean or null), infers a JSON schema for that JSON.resolveJsonSchemaOutput(JsonSchemaOutput output, IJsonSchemaTransformer transformer) Resolves aJsonSchemaOutputto a JSON schema string.Gets a builder for transforming JSON schemas.validateJson(Reader reader) Validates whether a given string represents a valid JSON value.validateJsonSchema(Reader reader) Validates whether the given JSON text is a valid JSON schema, according to the JSON Schema specification Draft 2020/12.
-
Method Details
-
constructJsonSchemaFromPropertyList
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 thetypeof 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 aIValueDescriptorfrom 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
anyvalue 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
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.
-
resolveJsonSchemaOutput
String resolveJsonSchemaOutput(JsonSchemaOutput output, IJsonSchemaTransformer transformer) throws JsonSchemaServiceException Resolves aJsonSchemaOutputto a JSON schema string. This method is a convenience method that calls the appropriate method based on thetypeof the givenJsonSchemaOutput. If the given output is null, returns a JSON schema that allows any value.- Parameters:
output- The output to resolve.transformer- The optional transformer to apply to the resolved schema, ifforbidSchemaAdjustmentsis disabled. May be null, in which case no transformation is applied.- Returns:
- A JSON schema as a string.
- Throws:
JsonSchemaServiceException- When the schema could not be resolved 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 theIJsonSchemaTransformerBuilder.build()to obtain the transformer instance, then calltransformto transform a JSON schema.- Returns:
- A builder for transforming JSON schemas.
-
validateJson
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
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, aIJsonValidationResultwith an appropriate error message is returned.- Parameters:
reader- The reader to read the JSON schema from.- Returns:
- The result of the validation.
-