Skip to content

Commit

Permalink
H-3337: Align type-system data-type constraints with Node API (Part I…
Browse files Browse the repository at this point in the history
…I) (#5239)
  • Loading branch information
TimDiekmann authored Sep 27, 2024
1 parent 9fdaa3d commit 89606b9
Show file tree
Hide file tree
Showing 33 changed files with 1,557 additions and 662 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
DataType,
EntityType,
OneOfSchema,
PropertyType,
Expand All @@ -12,7 +13,6 @@ import { atLeastOne, extractVersion } from "@blockprotocol/type-system";
import { typedEntries } from "@local/advanced-types/typed-entries";
import type {
BaseUrl,
CustomDataType,
EntityTypeMetadata,
} from "@local/hash-graph-types/ontology";
import type { Subgraph } from "@local/hash-subgraph";
Expand All @@ -29,7 +29,7 @@ import {

import { generateSimplifiedTypeId } from "../infer-entities/shared/generate-simplified-type-id.js";

type MinimalDataType = Omit<CustomDataType, "$id" | "$schema" | "kind">;
type MinimalDataType = Omit<DataType, "$id" | "$schema" | "kind" | "allOf">;

type MinimalPropertyObject = PropertyValueObject<
ValueOrArray<DereferencedPropertyType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath } from "node:url";

import type {
Conversions,
DataType,
DataTypeReference,
EntityType,
OneOfSchema,
Expand Down Expand Up @@ -33,7 +34,6 @@ import type { PropertyObjectWithMetadata } from "@local/hash-graph-types/entity"
import type {
BaseUrl,
ConstructDataTypeParams,
CustomDataType,
DataTypeWithMetadata,
EntityTypeWithMetadata,
PropertyTypeWithMetadata,
Expand Down Expand Up @@ -413,7 +413,9 @@ type BaseCreateTypeIfNotExistsParameters = {
export const generateSystemDataTypeSchema = ({
dataTypeId,
...rest
}: ConstructDataTypeParams & { dataTypeId: VersionedUrl }): CustomDataType => {
}: ConstructDataTypeParams & {
dataTypeId: VersionedUrl;
}): DataType => {
return {
$id: dataTypeId,
$schema: DATA_TYPE_META_SCHEMA,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DataType } from "@blockprotocol/type-system";
import type { IconDefinition } from "@fortawesome/free-solid-svg-icons";
import {
fa100,
Expand All @@ -14,7 +15,6 @@ import {
faSquareCheck,
faText,
} from "@hashintel/design-system";
import type { CustomDataType } from "@local/hash-graph-types/ontology";

import type { CustomIcon } from "../../../../../../../../../components/grid/utils/custom-grid-icons";
import type { EditorType } from "./types";
Expand Down Expand Up @@ -87,7 +87,7 @@ const identifierTypeTitles = ["URL", "URI"];

export const getEditorSpecs = (
editorType: EditorType,
dataType?: CustomDataType,
dataType?: DataType,
): EditorSpec => {
switch (editorType) {
case "boolean":
Expand Down
2 changes: 1 addition & 1 deletion apps/hash-frontend/src/pages/shared/data-types-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const DataTypesContextProvider = ({ children }: PropsWithChildren) => {
]?.inner.schema as DataTypeWithMetadata["schema"] | undefined;

if (!dataType) {
return formatDataValue(value?.toString() ?? "null", null);
return formatDataValue(value?.toString() ?? "null", undefined);
}

return formatDataValue(value, dataType);
Expand Down
2 changes: 1 addition & 1 deletion apps/hash-graph/libs/api/openapi/models/data_type.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions apps/hash-graph/libs/api/src/rest/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ enum LoadExternalDataTypeRequest {
#[serde(rename_all = "camelCase")]
Create {
#[schema(value_type = VAR_DATA_TYPE)]
schema: DataType,
schema: Box<DataType>,
relationships: Vec<DataTypeRelationAndSubject>,
#[serde(
default,
Expand Down Expand Up @@ -373,7 +373,7 @@ where
Ok(Json(
store
.create_data_type(actor_id, CreateDataTypeParams {
schema,
schema: *schema,
classification: OntologyTypeClassificationMetadata::External {
fetched_at: OffsetDateTime::now_utc(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
]
}
},
"required": ["$schema", "kind", "$id", "title", "type"],
"required": ["$schema", "kind", "$id", "title"],
"additionalProperties": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
]
}
},
"required": ["$schema", "kind", "title", "type"],
"required": ["$schema", "kind", "title"],
"additionalProperties": true
}
4 changes: 2 additions & 2 deletions libs/@blockprotocol/type-system/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
# Public workspace dependencies
error-stack = { workspace = true, public = true, features = ["unstable"]}
error-stack = { workspace = true, public = true, features = ["unstable"] }
codec = { workspace = true, public = true, features = ["serde"] }

# Public third-party dependencies
bytes = { workspace = true, public = true }
Expand All @@ -29,7 +30,6 @@ utoipa = { workspace = true, public = true, features = ["url"], optional = true
uuid = { workspace = true, public = true, features = ["std"] }

# Private workspace dependencies
codec = { workspace = true, features = ["serde"] }
futures = { workspace = true }

# Private third-party dependencies
Expand Down
8 changes: 1 addition & 7 deletions libs/@blockprotocol/type-system/rust/src/schema/array/raw.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
use serde::{Deserialize, Serialize};

/// Will serialize as a constant value `"array"`
#[derive(Serialize, Deserialize)]
#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))]
#[serde(rename_all = "camelCase")]
enum ArrayTypeTag {
Array,
}
use crate::schema::data_type::ArrayTypeTag;

#[derive(Deserialize)]
#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use error_stack::{Report, ReportSink, ResultExt};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

use crate::schema::{ConstraintError, ValueLabel, data_type::constraint::SimpleTypedValueSchema};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AnyOfConstraints {
#[cfg_attr(
target_arch = "wasm32",
tsify(type = "[SimpleTypedValueSchema, ...SimpleTypedValueSchema[]]")
)]
pub any_of: Vec<SimpleTypedValueSchema>,
}

impl AnyOfConstraints {
/// Checks if the provided value is valid against any of the schemas in the `any_of` list.
///
/// # Errors
///
/// - [`AnyOf`] if the value is not valid against any of the schemas.
///
/// [`AnyOf`]: ConstraintError::AnyOf
pub fn validate_value(&self, value: &JsonValue) -> Result<(), Report<ConstraintError>> {
let mut status = ReportSink::<ConstraintError>::new();
for schema in &self.any_of {
if let Err(error) = schema.constraints.validate_value(value) {
status.capture(error);
} else {
return Ok(());
}
}
status.finish().change_context(ConstraintError::AnyOf)
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AnyOfSchema {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "ValueLabel::is_empty")]
pub label: ValueLabel,
#[serde(flatten)]
pub constraints: AnyOfConstraints,
}
Loading

0 comments on commit 89606b9

Please sign in to comment.