public static Builder jsonSchema() { return new Builder(); }
public static Builder jsonSchemaFrom(final Class<?> pojo) { return new PojoJsonSchemaBuilder().create(pojo); }
public JsonSchema build() { final JsonSchema jsonSchema = new JsonSchema(); jsonSchema.setId(id); jsonSchema.setTitle(title); jsonSchema.setDescription(description); jsonSchema.setSchema(schema); jsonSchema.setRef(ref); jsonSchema.setType(type); jsonSchema.setMinItems(minItems); jsonSchema.setMaxItems(maxItems); jsonSchema.setMinLength(minLength); jsonSchema.setMaxLength(maxLength); jsonSchema.setMinimum(minimum); jsonSchema.setMaximum(maximum); jsonSchema.setUniqueItems(uniqueItems); jsonSchema.setPattern(pattern); jsonSchema.setDefaultValue(defaultValue); jsonSchema.setRequired(required); jsonSchema.setProperties(properties); jsonSchema.setEnumValues(enumValues); jsonSchema.setItems(items); return jsonSchema; } }
@Override public CompletionStage<PropertyContext<?>> convert(final CompletionStage<PropertyContext<?>> cs) { return cs.thenCompose(context -> { jsonSchema.setType(context.getProperty().getType().toLowerCase(ROOT)); final String prefix = context.getProperty().getPath() + "[]"; final List<SimplePropertyDefinition> arrayElements = final JsonSchema items = new JsonSchema(); items.setType("object"); items.setProperties(new HashMap<>()); jsonSchema.setItems(items); return CompletableFuture .allOf(arrayElements } else if (!arrayElements.isEmpty()) { // primitive final String type = arrayElements.get(0).getType(); final JsonSchema item = new JsonSchema(); item.setTitle(jsonSchema.getTitle()); item.setType("enum".equalsIgnoreCase(type) ? "string" : type.toLowerCase(ROOT)); jsonSchema.setItems(item);
ui.setUiSchema(new ArrayList<>()); ui.setProperties(new HashMap<>()); ui.setJsonSchema(new JsonSchema()); ui.getJsonSchema().setTitle(displayName.get()); ui.getJsonSchema().setType("object"); ui .getJsonSchema() .setRequired(props .stream() .filter(isRootProperty)
ofNullable(validation.getMin()).ifPresent(m -> jsonSchema.setMinimum(m.doubleValue())); ofNullable(validation.getMax()).ifPresent(m -> jsonSchema.setMaximum(m.doubleValue())); ofNullable(validation.getMinItems()).ifPresent(jsonSchema::setMinItems); ofNullable(validation.getMaxItems()).ifPresent(jsonSchema::setMaxItems); if (rootJsonSchema.getProperties() == null) { rootJsonSchema.setProperties(new HashMap<>()); rootJsonSchema.getProperties().put(context.getProperty().getName(), jsonSchema); final String order = context.getProperty().getMetadata().get("ui::optionsorder::value"); if (order != null) { jsonSchema.setProperties(new TreeMap<>(new Comparator<String>() { jsonSchema.setProperties(new HashMap<>());
@Override public CompletionStage<PropertyContext<?>> convert(final CompletionStage<PropertyContext<?>> cs) { return cs.thenCompose(context -> { final JsonSchema jsonSchema = new JsonSchema(); jsonSchema.setTitle(context.getProperty().getDisplayName()); final String type = context.getProperty().getType(); switch (type.toLowerCase(ROOT)) { return CompletableFuture.completedFuture(context); jsonSchema.setType(type.toLowerCase(ROOT)); of(context .findDirectChild(properties)
@Override public CompletionStage<PropertyContext<?>> convert(final CompletionStage<PropertyContext<?>> cs) { return cs.thenCompose(context -> { final UiSchema schema = newUiSchema(context); schema.setWidget("datalist"); schema.setTitleMap(emptyList()); final JsonSchema jsonSchema = findJsonSchema(context); if (jsonSchema != null) { if (jsonSchema.getType() == null) { jsonSchema.setType("string"); } // avoid to validate against the enum since we don't udpate it at suggestion time jsonSchema.setEnumValues(null); } return CompletableFuture.completedFuture(context); }); } }
public JsonSchema.Builder create(final Class<?> pojo) { final JsonSchema.Builder builder = jsonSchema().withTitle(pojo.getSimpleName()).withType("object"); Class<?> current = pojo; final Collection<String> excludes = new HashSet<>(); while (current != Object.class && current != null) { Stream.of(current.getDeclaredFields()).filter(f -> !f.getName().equals("$jacocoData")).filter(f -> { final int modifiers = f.getModifiers(); return !Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers); }) .filter(f -> !f.isAnnotationPresent(JsonSchemaIgnore.class)) .filter(f -> excludes.add(f.getName())) .sorted(comparing(Field::getName)) .forEach(field -> { final String name = ofNullable(field.getAnnotation(JsonSchemaProperty.class)) .map(JsonSchemaProperty::value) .orElseGet(field::getName); builder.withProperty(name, buildSchema(field)); }); current = current.getSuperclass(); } return builder; }
return schemas.computeIfAbsent(Class.class.cast(genericType), k -> jsonSchema().withType("string").build()); } else if (genericType == long.class || genericType == Long.class || genericType == int.class || genericType == Integer.class || genericType == byte.class || genericType == Byte.class || genericType == Double.class || genericType == float.class || genericType == Float.class || genericType == BigDecimal.class || genericType == BigInteger.class) { return schemas.computeIfAbsent(Class.class.cast(genericType), k -> jsonSchema().withType("number").build()); } else if (genericType == boolean.class || genericType == Boolean.class) { return schemas .computeIfAbsent(Class.class.cast(genericType), k -> jsonSchema().withType("boolean").build()); } else if (Class.class.isInstance(genericType)) { final Class<?> clazz = Class.class.cast(genericType); return ofNullable(schemas.get(clazz)).orElseGet(() -> { final JsonSchema jsonSchema = create(clazz).build(); schemas.put(clazz, jsonSchema); return jsonSchema; final JsonSchema jsonSchema = create(itemClass).build(); schemas.put(itemClass, jsonSchema); return jsonSchema; }); return jsonSchema().withType("array").withItems(nested).build(); } else if (Map.class.isAssignableFrom(rawClazz) && pt.getActualTypeArguments().length == 2) { final Type keyType = pt.getActualTypeArguments()[0]; return jsonSchema().withType("object").build(); } else { throw new IllegalArgumentException(
@Override public CompletionStage<PropertyContext<?>> convert(final CompletionStage<PropertyContext<?>> cs) { return cs.thenApply(context -> { jsonSchema.setType("string"); if (context.getProperty().getValidation() == null || context.getProperty().getValidation().getEnumValues() == null) { jsonSchema.setEnumValues(emptyList()); } else { jsonSchema.setEnumValues(context.getProperty().getValidation().getEnumValues()); } return context; }); } }
protected JsonSchema findJsonSchema(final PropertyContext<?> cs) { final String[] segments = cs.getProperty().getPath().split("\\."); JsonSchema schema = jsonSchema; for (final String current : segments) { if (current.endsWith("[]")) { schema = schema.getProperties().get(current.substring(0, current.length() - "[]".length())).getItems(); } else { schema = schema.getProperties().get(current); } if (schema != null && "array".equals(schema.getType()) && schema.getItems() != null) { schema = schema.getItems(); } if (schema == null) { // unexpected log.warn("Didn't find json schema for {}", cs.getProperty().getPath()); return null; } } return schema; } }
@Override public CompletionStage<PropertyContext<?>> convert(final CompletionStage<PropertyContext<?>> cs) { return cs.thenCompose(context -> { final UiSchema schema = newUiSchema(context); setupUiSchema(context, schema); final JsonSchema jsonSchema = findJsonSchema(context); if (jsonSchema == null) { // unexpected return CompletableFuture.completedFuture(context); } if (jsonSchema.getType() == null) { jsonSchema.setType("string"); } return fillProposalsAndReturn(context, schema, jsonSchema); }); }
@CacheResult(cacheName = "org.talend.sdk.component.proxy.validation.jsonschema") public CompletionStage<JsonSchemaValidator> getValidator(final RequestContext context, final String formId) { return configurations.getDetails(context.language(), formId, context::findPlaceholder).thenCompose(config -> { final JsonSchema jsonSchema = new JsonSchema(); final JsonSchemaConverter converter = new JsonSchemaConverter(jsonb, jsonSchema, config.getProperties()); return CompletableFuture .allOf(config .getProperties() .stream() .filter(Objects::nonNull) .filter(p -> p.getName().equals(p.getPath())) .map(it -> new PropertyContext<>(it, context, LIGHT_CONTEXT_CONFIGURATION)) .map(CompletionStages::toStage) .map(converter::convert) .toArray(CompletableFuture[]::new)) .thenApply(r -> jsonSchema); }).thenApply(schema -> jsonb.fromJson(jsonb.toJson(schema), JsonObject.class)).thenApply(factory::newInstance); } }
return nameValue; }).collect(toList())); jsonSchema.setEnumValues(context.getProperty().getValidation().getEnumValues()); } else { final String actionName = context.getProperty().getMetadata().get("action::dynamic_values"); return pairs.thenApply(namedValues -> { schema.setTitleMap(namedValues); jsonSchema.setEnumValues(namedValues.stream().map(UiSchema.NameValue::getValue).collect(toList())); return context; });
if (jsonSchema.getType() == null) { jsonSchema.setType("string"); return pairs.thenApply(namedValues -> { schema.setTitleMap(namedValues); jsonSchema.setEnumValues(namedValues.stream().map(UiSchema.NameValue::getValue).collect(toList())); return context; });