Skip to content

Instantly share code, notes, and snippets.

@eHorn96
Last active February 25, 2025 12:12
Show Gist options
  • Select an option

  • Save eHorn96/994d474940ff4f2facf92b65e167f24b to your computer and use it in GitHub Desktop.

Select an option

Save eHorn96/994d474940ff4f2facf92b65e167f24b to your computer and use it in GitHub Desktop.
JSON:Siren specification in Pydantic
# generated by datamodel-codegen:
# filename: schema.siren.json
# timestamp: 2025-02-18T14:12:41+00:00
from __future__ import annotations
from typing import Annotated, Any, Dict, List, Literal
from devtools import debug
from pydantic import AnyUrl, BaseModel, Field
class EmbeddedLinkSubEntity(BaseModel):
class_: Annotated[
List[str] | None,
Field(
description="Describes the nature of an entity's content "
"based on the current representation. "
"Possible values are implementation-dependent "
"and should be documented.",
),
] = None
rel: Annotated[
List[str],
Field(
description="Defines the relationship of the sub-entity "
"to its parent, per Web Linking (RFC5899).",
min_length=1,
),
]
href: Annotated[AnyUrl, Field(description="The URI of the linked sub-entity.")]
type: Annotated[
str | None,
Field(
description="Defines media type of the linked sub-entity, "
"per Web Linking (RFC5899)."
),
] = None
title: Annotated[
str | None, Field(description="Descriptive text about the entity.")
] = None
class FieldValueObject(BaseModel):
title: Annotated[
str | None, Field(description="Textual description of a field value.")
] = None
value: Annotated[str | float, Field(description="Possible value for the field.")]
selected: Annotated[
bool | None,
Field(
description='A value object with a `"selected" = true` attribute indicates that this value should be considered preselected by the client. When missing, the default value is `false`.'
),
] = False
class Link(BaseModel):
class_: Annotated[
List[str] | None,
Field(
description="Describes aspects of the link based on the current representation. Possible values are implementation-dependent and should be documented.",
),
] = None
title: Annotated[
str | None, Field(description="Text describing the nature of a link.")
] = None
rel: Annotated[
List[str],
Field(
description="Defines the relationship of the link to its entity, per Web Linking (RFC5988)."
),
]
href: str = Field(description="The URI of the linked resource.")
type: Annotated[
str | None,
Field(
description="Defines media type of the linked resource, per Web Linking (RFC5988)."
),
] = None
class FieldModel(BaseModel):
name: Annotated[
str,
Field(
description="A name describing the control. Field names MUST be unique within the set of fields for an action. The behaviour of clients when parsing a Siren document that violates this constraint is undefined."
),
]
type: Annotated[
Literal[
"hidden",
"text",
"search",
"tel",
"url",
"email",
"password",
"datetime",
"date",
"month",
"week",
"time",
"datetime-local",
"number",
"range",
"color",
"checkbox",
"radio",
"file",
]
| None,
Field(
description="The input type of the field. This is a subset of the input types specified by HTML5."
),
] = "text"
title: Annotated[
str | None,
Field(
description="Textual annotation of a field. Clients may use this as a label."
),
] = None
value: Annotated[
str | float | List[FieldValueObject] | None,
Field(
description="A value assigned to the field. May be a scalar value or a list of value objects."
),
] = None
class Action(BaseModel):
class_: Annotated[
List[str] | None,
Field(
description="Describes the nature of an action based on the current representation. Possible values are implementation-dependent and should be documented.",
),
] = None
name: Annotated[
str,
Field(
description="A string that identifies the action to be performed. Action names MUST be unique within the set of actions for an entity. The behaviour of clients when parsing a Siren document that violates this constraint is undefined."
),
]
method: Annotated[
str | None,
Field(
description="An enumerated attribute mapping to a protocol method. For HTTP, these values may be GET, PUT, POST, DELETE, or PATCH. As new methods are introduced, this list can be extended. If this attribute is omitted, GET should be assumed."
),
] = "GET"
href: Annotated[AnyUrl, Field(description="The URI of the action.")]
title: Annotated[
str | None, Field(description="Descriptive text about the action.")
] = None
type: Annotated[
str | None,
Field(
description="The encoding type for the request. When omitted and the fields attribute exists, the default value is `application/x-www-form-urlencoded`."
),
] = "application/x-www-form-urlencoded"
fields: Annotated[
List[FieldModel] | None, Field(description="A collection of fields.")
] = None
class Entity(BaseModel):
class_: Annotated[
List[str] | None,
Field(
description="Describes the nature of an entity's content"
" based on the current representation. "
"Possible values are implementation-dependent "
"and should be documented.",
),
] = None
title: Annotated[
str | None, Field(description="Descriptive text about the entity.")
] = None
properties: Annotated[
Dict[str, Any] | None,
Field(
description="A set of key-value pairs that describe the state of an entity."
),
] = None
entities: Annotated[
List[EmbeddedLinkSubEntity | EmbeddedRepresentationSubEntity] | None,
Field(
description="A collection of related sub-entities."
" If a sub-entity contains an href value,"
" it should be treated as an embedded link. "
"Clients may choose to optimistically load "
"embedded links. If no href value exists,"
" the sub-entity is an embedded entity"
" representation that contains all the"
" characteristics of a typical entity."
" One difference is that a sub-entity "
"MUST contain a rel attribute to describe"
" its relationship to the parent entity."
),
] = None
actions: Annotated[
List[Action] | None,
Field(
description="A collection of actions;"
" actions show available "
"behaviors an entity exposes."
),
] = None
links: Annotated[
List[Link] | None,
Field(
description="A collection of items"
" that describe navigational links,"
" distinct from entity relationships."
" Link items should contain a `rel`"
" attribute to describe the relationship "
"and an `href` attribute to point"
" to the target URI. Entities should"
" include a link `rel` to `self`."
),
] = None
class EmbeddedRepresentationSubEntity(Entity):
rel: Annotated[
List[str],
Field(
default_factory=lambda: list,
description="Defines the relationship of"
" the sub-entity to its parent,"
" per Web Linking (RFC5899).",
),
]
# Enables self-reference
Entity.model_rebuild()
EmbeddedRepresentationSubEntity.model_rebuild()
if __name__ == "__main__":
debug(Entity.model_json_schema())
@eHorn96
Copy link
Author

eHorn96 commented Feb 25, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment