Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H-3337: Align type-system data-type constraints with Node API #5180

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ jobs:
working-directory: ${{ matrix.directory }}
env:
RUSTDOCFLAGS: "--check -Z unstable-options -D warnings"
run: just doc
run: cargo doc --all-features --no-deps -Zrustdoc-scrape-examples

- name: Check private documentation
if: always() && steps.lints.outputs.has-rust == 'true'
working-directory: ${{ matrix.directory }}
env:
RUSTDOCFLAGS: "--check -Z unstable-options -D warnings"
run: just doc --document-private-items
run: cargo doc --all-features --no-deps -Zrustdoc-scrape-examples --document-private-items

- name: Show disk usage
run: df -h
Expand Down
7 changes: 0 additions & 7 deletions .justfile
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,6 @@ clippy *arguments: install-cargo-hack
@just in-pr cargo clippy --profile {{profile}} --all-features --all-targets --no-deps {{arguments}}
@just not-in-pr cargo hack --optional-deps --feature-powerset clippy --profile {{profile}} --all-targets --no-deps {{arguments}}

# Creates the documentation for the crate
[no-cd]
doc *arguments:
@just in-pr RUSTDOCFLAGS=-Dwarnings cargo doc --all-features --workspace --no-deps -Zunstable-options -Zrustdoc-scrape-examples {{arguments}}
@just not-in-pr cargo doc --all-features --workspace --no-deps -Zunstable-options -Zrustdoc-scrape-examples {{arguments}}


# Builds the crate
[no-cd]
build *arguments:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ArraySchema } from "@blockprotocol/type-system";
import type { PropertyValueArray } from "@blockprotocol/type-system";
import type { JSONSchemaDefinition } from "openai/lib/jsonschema";

import type { DereferencedPropertyType } from "../../shared/dereference-entity-type.js";
Expand All @@ -12,7 +12,7 @@ import type { DereferencedPropertyType } from "../../shared/dereference-entity-t
export const stripIdsFromDereferencedProperties = (params: {
properties: Record<
string,
DereferencedPropertyType | ArraySchema<DereferencedPropertyType>
DereferencedPropertyType | PropertyValueArray<DereferencedPropertyType>
>;
}): {
[key: string]: JSONSchemaDefinition;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type {
ArraySchema,
EntityType,
ObjectSchema,
OneOfSchema,
PropertyType,
PropertyValueArray,
PropertyValueObject,
PropertyValues,
ValueOrArray,
VersionedUrl,
Expand Down Expand Up @@ -31,14 +31,14 @@ import { generateSimplifiedTypeId } from "../infer-entities/shared/generate-simp

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

type MinimalPropertyObject = ObjectSchema<
type MinimalPropertyObject = PropertyValueObject<
ValueOrArray<DereferencedPropertyType>
> & { additionalProperties: false };

export type MinimalPropertyTypeValue =
| MinimalDataType
| MinimalPropertyObject
| ArraySchema<OneOfSchema<MinimalPropertyTypeValue>>;
| PropertyValueArray<OneOfSchema<MinimalPropertyTypeValue>>;

export type DereferencedPropertyType = Pick<
PropertyType,
Expand All @@ -57,7 +57,7 @@ export type DereferencedEntityType<
> = Pick<EntityType, "$id" | "description" | "links" | "required" | "title"> & {
properties: Record<
PropertyTypeKey,
DereferencedPropertyType | ArraySchema<DereferencedPropertyType>
DereferencedPropertyType | PropertyValueArray<DereferencedPropertyType>
>;
additionalProperties: false;
} & Pick<EntityTypeMetadata, "labelProperty">;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

import type {
ArraySchema,
Conversions,
DataTypeReference,
EntityType,
ObjectSchema,
OneOfSchema,
PropertyType,
PropertyTypeReference,
PropertyValueArray,
PropertyValueObject,
PropertyValues,
ValueOrArray,
VersionedUrl,
Expand Down Expand Up @@ -362,7 +362,7 @@ export const generateSystemPropertyTypeSchema = (
};
inner = dataTypeReference;
} else if (propertyTypeObjectProperties) {
const propertyTypeObject: ObjectSchema<
const propertyTypeObject: PropertyValueObject<
ValueOrArray<PropertyTypeReference>
> = {
type: "object" as const,
Expand All @@ -380,13 +380,14 @@ export const generateSystemPropertyTypeSchema = (

// Optionally wrap inner in an array
if (array) {
const arrayOfPropertyValues: ArraySchema<OneOfSchema<PropertyValues>> =
{
type: "array",
items: {
oneOf: [inner],
},
};
const arrayOfPropertyValues: PropertyValueArray<
OneOfSchema<PropertyValues>
> = {
type: "array",
items: {
oneOf: [inner],
},
};
return arrayOfPropertyValues;
} else {
return inner;
Expand Down
12 changes: 7 additions & 5 deletions apps/hash-frontend/src/lib/typeguards.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
ArraySchema,
ObjectSchema,
OneOfSchema,
PropertyTypeReference,
PropertyValueArray,
PropertyValueObject,
PropertyValues,
ValueOrArray,
} from "@blockprotocol/type-system";
Expand All @@ -13,12 +13,14 @@ export function isNonNullable<T>(value: T): value is NonNullable<T> {

export const isPropertyValueArray = (
propertyValue: PropertyValues,
): propertyValue is ArraySchema<OneOfSchema<PropertyValues>> => {
): propertyValue is PropertyValueArray<OneOfSchema<PropertyValues>> => {
return "type" in propertyValue && propertyValue.type === "array";
};

export const isPropertyValuePropertyObject = (
export const isPropertyValueObject = (
propertyValue: PropertyValues,
): propertyValue is ObjectSchema<ValueOrArray<PropertyTypeReference>> => {
): propertyValue is PropertyValueObject<
ValueOrArray<PropertyTypeReference>
> => {
return "type" in propertyValue && propertyValue.type === "object";
};
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ export const NumberOrTextInput = ({
const step =
"multipleOf" in expectedType && expectedType.multipleOf !== undefined
? expectedType.multipleOf
: expectedType.type === "integer"
? 1
: 0.01;
: 0.01;

const exclusiveMinimum =
"exclusiveMinimum" in expectedType &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const guessEditorTypeFromExpectedType = (
throw new Error("Array data types are not yet handled.");
}

return dataType.type === "integer" ? "number" : dataType.type;
return dataType.type;
};

export const isBlankStringOrNullish = (value: unknown) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import get from "lodash/get";

import {
isPropertyValueArray,
isPropertyValuePropertyObject,
isPropertyValueObject,
} from "../../../../../../../../../lib/typeguards";
import type { PropertyRow } from "../../types";
import { getExpectedTypesOfPropertyType } from "./get-expected-types-of-property-type";
Expand Down Expand Up @@ -92,7 +92,7 @@ export const generatePropertyRowRecursively = ({
const children: PropertyRow[] = [];

const firstOneOf = propertyType.oneOf[0];
const isFirstOneOfNested = isPropertyValuePropertyObject(firstOneOf);
const isFirstOneOfNested = isPropertyValueObject(firstOneOf);
const isFirstOneOfArray = isPropertyValueArray(firstOneOf);

// if first `oneOf` of property type is nested property, it means it should have children
Expand Down
8 changes: 4 additions & 4 deletions apps/hash-graph/libs/store/src/data_type/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub enum DataTypeQueryPath<'p> {
///
/// [`DataType::title()`]: type_system::schema::DataType::title
Title,
/// Corresponds to [`DataType::description()`]
/// Corresponds to [`DataType`]'s description.
///
/// ```rust
/// # use serde::Deserialize;
Expand All @@ -137,9 +137,9 @@ pub enum DataTypeQueryPath<'p> {
/// # Ok::<(), serde_json::Error>(())
/// ```
///
/// [`DataType::description()`]: type_system::schema::DataType::description
/// [`DataType`]: type_system::schema::DataType
Description,
/// Corresponds to [`DataType::json_type()`].
/// Corresponds to [`DataType`]'s type.
///
/// ```rust
/// # use serde::Deserialize;
Expand All @@ -150,7 +150,7 @@ pub enum DataTypeQueryPath<'p> {
/// # Ok::<(), serde_json::Error>(())
/// ```
///
/// [`DataType::json_type()`]: type_system::schema::DataType::json_type
/// [`DataType`]: type_system::schema::DataType
Type,
/// Only used internally and not available for deserialization.
OntologyId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const compileIndividualSchemaToTypescript = async (
type: DataType | PropertyType | EntityType | JsonSchema,
context: CompileContext,
): Promise<CompiledTsType> =>
compileJsonSchema(type, type.title, {
compileJsonSchema(type as unknown as JsonSchema, type.title, {
bannerComment: "",
enableConstEnums: true,
declareExternallyReferenced: true,
Expand Down
12 changes: 6 additions & 6 deletions libs/@blockprotocol/type-system/rust/src/schema/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use serde::{Deserialize, Serialize, Serializer};
use crate::schema::{OneOfSchema, PropertyType, PropertyValues};

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(from = "raw::ArraySchema<T>")]
pub struct ArraySchema<T> {
#[serde(from = "raw::PropertyValueArray<T>")]
pub struct PropertyValueArray<T> {
pub items: T,
pub min_items: Option<usize>,
pub max_items: Option<usize>,
}

impl<T> Serialize for ArraySchema<T>
impl<T> Serialize for PropertyValueArray<T>
where
T: Serialize,
{
Expand All @@ -30,7 +30,7 @@ where
#[serde(untagged)]
pub enum ValueOrArray<T> {
Value(T),
Array(ArraySchema<T>),
Array(PropertyValueArray<T>),
}

pub trait PropertyArraySchema {
Expand All @@ -39,7 +39,7 @@ pub trait PropertyArraySchema {
fn max_items(&self) -> Option<usize>;
}

impl PropertyArraySchema for ArraySchema<OneOfSchema<PropertyValues>> {
impl PropertyArraySchema for PropertyValueArray<OneOfSchema<PropertyValues>> {
fn possibilities(&self) -> &[PropertyValues] {
&self.items.possibilities
}
Expand All @@ -53,7 +53,7 @@ impl PropertyArraySchema for ArraySchema<OneOfSchema<PropertyValues>> {
}
}

impl PropertyArraySchema for ArraySchema<&PropertyType> {
impl PropertyArraySchema for PropertyValueArray<&PropertyType> {
fn possibilities(&self) -> &[PropertyValues] {
&self.items.one_of
}
Expand Down
10 changes: 5 additions & 5 deletions libs/@blockprotocol/type-system/rust/src/schema/array/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ enum ArrayTypeTag {
#[derive(Deserialize)]
#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(super) struct ArraySchema<T> {
pub(super) struct PropertyValueArray<T> {
#[serde(rename = "type")]
_type: ArrayTypeTag,
items: T,
Expand All @@ -21,8 +21,8 @@ pub(super) struct ArraySchema<T> {
max_items: Option<usize>,
}

impl<T> From<ArraySchema<T>> for super::ArraySchema<T> {
fn from(object: ArraySchema<T>) -> Self {
impl<T> From<PropertyValueArray<T>> for super::PropertyValueArray<T> {
fn from(object: PropertyValueArray<T>) -> Self {
Self {
items: object.items,
min_items: object.min_items,
Expand All @@ -42,8 +42,8 @@ pub(super) struct ArraySchemaRef<'a, T> {
max_items: Option<usize>,
}

impl<'a, T> From<&'a super::ArraySchema<T>> for ArraySchemaRef<'a, T> {
fn from(object: &'a super::ArraySchema<T>) -> Self {
impl<'a, T> From<&'a super::PropertyValueArray<T>> for ArraySchemaRef<'a, T> {
fn from(object: &'a super::PropertyValueArray<T>) -> Self {
Self {
r#type: ArrayTypeTag::Array,
items: &object.items,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use crate::{schema::DataType, url::VersionedUrl, Valid};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))]
// #[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some follow-up to restore this? Or should it just be removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to re-add this. It's disabled here because Tsify creates a interface ClosedDataType extends DataType with DataType being a type, not an interface, and extending a type is not allowed. We don't expose ClosedDataType from the graph currently anyway, so currently it's unused. I decided to deal with this later.

pub struct ClosedDataType {
#[serde(flatten)]
pub schema: Arc<DataType>,
Expand Down
Loading
Loading