/** * Validate that the value can be used for this schema, i.e. that its type matches the schema type and optional * requirements. Throws a DataException if the value is invalid. * @param value the value to validate */ public void validateValue(Object value) { validateValue(this, value); }
/** * Validate that the value can be used with the schema, i.e. that its type matches the schema type and nullability * requirements. Throws a DataException if the value is invalid. * @param schema Schema to test * @param value value to test */ public static void validateValue(Schema schema, Object value) { validateValue(null, schema, value); }
/** * Set the default value for this schema. The value is validated against the schema type, throwing a * {@link SchemaBuilderException} if it does not match. * @param value the default value * @return the SchemaBuilder */ public SchemaBuilder defaultValue(Object value) { checkCanSet(DEFAULT_FIELD, defaultValue, value); checkNotNull(TYPE_FIELD, type, DEFAULT_FIELD); try { ConnectSchema.validateValue(this, value); } catch (DataException e) { throw new SchemaBuilderException("Invalid default value", e); } defaultValue = value; return this; }
/** * Set the value of a field. Validates the value, throwing a {@link DataException} if it does not match the field's * {@link Schema}. * @param field the field to set * @param value the value of the field * @return the Struct, to allow chaining of {@link #put(String, Object)} calls */ public Struct put(Field field, Object value) { if (null == field) throw new DataException("field cannot be null."); ConnectSchema.validateValue(field.name(), field.schema(), value); values[field.index()] = value; return this; }
List<?> array = (List<?>) value; for (Object entry : array) validateValue(schema.valueSchema(), entry); break; case MAP: Map<?, ?> map = (Map<?, ?>) value; for (Map.Entry<?, ?> entry : map.entrySet()) { validateValue(schema.keySchema(), entry.getKey()); validateValue(schema.valueSchema(), entry.getValue());
/** * Validates that this struct has filled in all the necessary data with valid values. For required fields * without defaults, this validates that a value has been set and has matching types/schemas. If any validation * fails, throws a DataException. */ public void validate() { for (Field field : schema.fields()) { Schema fieldSchema = field.schema(); Object value = values[field.index()]; if (value == null && (fieldSchema.isOptional() || fieldSchema.defaultValue() != null)) continue; ConnectSchema.validateValue(field.name(), fieldSchema, value); } }