Invoice Schema Module
The rdetoolkit.models.invoice_schema
module provides Pydantic models for validating and managing RDE invoice schema JSON structures. This module defines a comprehensive set of models that correspond to the JSON schema format used in RDE (Research Data Exchange) systems.
Overview
The invoice schema module implements a hierarchical validation system for dataset templates using Pydantic models. The module supports:
- Multi-language Labels: Support for Japanese and English labels
- Custom Field Validation: Flexible custom property definitions with type validation
- Sample Data Management: Structured handling of general and specific sample attributes
- Schema Validation: Comprehensive validation rules for JSON schema compliance
- Type Safety: Strong typing with runtime validation
Core Classes
InvoiceSchemaJson
The main class representing the complete invoice schema structure.
InvoiceSchemaJson Constructor
1 2 3 4 5 6 7 8 |
|
Parameters:
version
(str): JSON schema version (default: "https://json-schema.org/draft/2020-12/schema")schema_id
(str): Schema identifier URL (default: "https://rde.nims.go.jp/rde/dataset-templates/")description
(Optional[str]): Description of the schemavalue_type
(Literal["object"]): Schema type, must be "object"required
(Optional[list]): List of required top-level propertiesproperties
(Properties): Schema properties definition
Validation Rules (MetaProperty)
- If
"custom"
is inrequired
,properties.custom
must not be None - If
"sample"
is inrequired
,properties.sample
must not be None - If
properties.custom
exists,"custom"
must be inrequired
- If
properties.sample
exists,"sample"
must be inrequired
InvoiceSchemaJson Usage Example
1 2 3 4 5 6 7 8 9 10 11 |
|
Properties
Container for top-level schema properties.
Properties Constructor
1 2 3 4 |
|
Parameters:
custom
(Optional[CustomField]): Custom field definitionssample
(Optional[SampleField]): Sample field definitions
Properties Usage Example
1 2 3 4 5 6 |
|
Language Support
LangLabels
Represents labels in multiple languages (Japanese and English).
LangLabels Constructor
1 |
|
Parameters:
ja
(str): Japanese labelen
(str): English label
Placeholder Example
1 2 3 4 5 6 |
|
Placeholder
Represents placeholder text in multiple languages.
Placeholder Constructor
1 |
|
Parameters:
ja
(str): Japanese placeholder texten
(str): English placeholder text
LangLabels Example
1 2 3 4 5 6 |
|
Custom Fields
CustomField
Represents a custom field definition in the invoice schema.
CustomField Constructor
1 2 3 4 5 6 |
|
Parameters:
obj_type
(Literal["object"]): Field type, must be "object"label
(LangLabels): Multi-language labelrequired
(list[str]): List of required property namesproperties
(CustomItems): Custom property definitions
CustomItems
A dictionary-like container for custom property definitions.
CustomItems Constructor
1 |
|
Parameters:
root
(dict[str, MetaProperty]): Dictionary mapping property names to MetaProperty instances
Methods
__iter__()
1 |
|
Returns an iterator over the custom items.
__getitem__(item)
1 |
|
Access a custom property by name.
CustomItems Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
MetaProperty
Defines a custom property with validation rules.
MetaProperty Constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Parameters:
label
(LangLabels): Multi-language property labelvalue_type
(Literal): Data type of the property valuedescription
(Optional[str]): Property descriptionexamples
(Optional[list]): Example valuesdefault
(Optional[Union]): Default valueconst
(Optional[Union]): Constant value constraintenum
(Optional[list]): Enumerated allowed valuesmaximum
(Optional[int]): Maximum value (for numeric types)exclusiveMaximum
(Optional[int]): Exclusive maximum valueminimum
(Optional[int]): Minimum value (for numeric types)exclusiveMinimum
(Optional[int]): Exclusive minimum valuemaxLength
(Optional[int]): Maximum string lengthminLength
(Optional[int]): Minimum string lengthpattern
(Optional[str]): Regular expression patternformat
(Optional[Literal]): String format constraint
CustomField Validation Rules
- Numeric constraints (
maximum
,minimum
, etc.) only apply to"integer"
or"number"
types - String length constraints (
maxLength
,minLength
) only apply to"string"
type - If
const
is specified, its type must matchvalue_type
MetaProperty Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Options
Represents widget options for custom properties.
Options Constructor
1 2 3 4 5 6 |
|
Parameters:
widget
(Optional[Literal["textarea"]]): Widget typerows
(Optional[int]): Number of rows for textarea widgetunit
(Optional[str]): Unit of measurementplaceholder
(Optional[Placeholder]): Placeholder text
Validation Rules
- If
widget
is set to"textarea"
,rows
must be specified
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Sample Fields
SampleField
Represents the sample field definition in the invoice schema.
SampleField Constructor
1 2 3 4 5 6 |
|
Parameters:
obj_type
(Literal["object"]): Field type, must be "object"label
(LangLabels): Multi-language labelrequired
(list): Required property names (default: ["names", "sampleId"])properties
(SampleProperties): Sample property definitions
SampleProperties
Contains the properties for sample data.
SampleProperties Constructor
1 2 3 4 |
|
Parameters:
generalAttributes
(Optional[GeneralAttribute]): General sample attributesspecificAttributes
(Optional[SpecificAttribute]): Specific sample attributes
SamplePropertiesWhenAdding
Extended sample properties used when adding new samples.
SamplePropertiesWhenAdding Constructor
1 2 3 4 5 6 7 8 9 |
|
Parameters:
sample_id
(Optional[str]): Sample identifierownerId
(str): Owner ID (must match pattern^([0-9a-zA-Z]{56})$
)composition
(Optional[str]): Sample compositionreferenceUrl
(Optional[str]): Reference URLdescription
(Optional[str]): Sample descriptiongeneralAttributes
(Optional[GeneralAttribute]): General attributesspecificAttributes
(Optional[SpecificAttribute]): Specific attributes
Sample Attributes
GeneralAttribute
Represents general attributes for samples.
GeneralAttribute Constructor
1 2 3 4 |
|
Parameters:
obj_type
(Literal["array"]): Attribute type, must be "array"items
(SampleGeneralItems): General attribute items
SampleGeneralItems
Container for general attribute items.
SampleGeneralItems Constructor
1 |
|
Parameters:
root
(Optional[list[GeneralProperty]]): List of general properties
GeneralProperty
Defines a general property structure.
GeneralProperty Constructor
1 2 3 4 5 |
|
Parameters:
object_type
(Literal["object"]): Property type, must be "object"required
(list): Required fields (must include "termId" and "value")properties
(GeneralChildProperty): Child properties
GeneralChildProperty
Contains child properties for general attributes.
GeneralChildProperty Constructor
1 |
|
Parameters:
term_id
(TermId): Term identifier
SpecificAttribute
Represents specific attributes for samples.
SpecificAttribute Constructor
1 2 3 4 |
|
Parameters:
obj_type
(Literal["array"]): Attribute type, must be "array"items
(SampleSpecificItems): Specific attribute items
SampleSpecificItems
Container for specific attribute items.
SampleSpecificItems Constructor
1 |
|
Parameters:
root
(list[SpecificProperty]): List of specific properties
SpecificProperty
Defines a specific property structure.
SpecificProperty Constructor
1 2 3 4 5 |
|
Parameters:
object_type
(Literal["object"]): Property type, must be "object"required
(list): Required fields (must include "classId", "termId", and "value")properties
(SpecificChildProperty): Child properties
SpecificChildProperty
Contains child properties for specific attributes.
SpecificChildProperty Constructor
1 2 3 4 |
|
Parameters:
term_id
(TermId): Term identifierclass_id
(ClassId): Class identifier
Identifier Classes
TermId
Represents a term identifier with a constant value.
TermId Constructor
1 |
|
Parameters:
const
(str): Constant term identifier value
ClassId
Represents a class identifier with a constant value.
ClassId Constructor
1 |
|
Parameters:
const
(str): Constant class identifier value
Basic Data Types
DatasetId
Represents a dataset identifier.
DatasetId Constructor
1 |
|
Parameters:
value_type
(str): Data type, defaults to "string"
BasicItems
Contains basic invoice items with predefined structures.
BasicItems Constructor
1 2 3 4 5 6 7 8 |
|
BasicItemsValue
Represents a basic value type with validation.
BasicItemsValue Constructor
1 2 3 4 5 6 |
|
Parameters:
value_type
(Union[str, list, None]): Value type specificationformat
(Optional[Literal["date"]]): Date format constraintpattern
(Optional[str]): Regular expression patterndescription
(Optional[str]): Value description
Complete Usage Examples
Creating a Custom Field Schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
Creating a Sample Field Schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
Validation and Error Handling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Working with Enum and Const Values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Error Handling
Common Validation Errors
The invoice schema models may raise ValidationError
exceptions in the following cases:
Type Mismatch Errors
1 2 3 4 5 6 7 8 |
|
Required Field Errors
1 2 3 4 5 6 7 |
|
Pattern Validation Errors
1 2 3 4 5 6 |
|
Best Practices
- Use Type Hints: Always use proper type hints for better IDE support and validation:
1 2 3 4 5 6 7 8 |
|
- Validate Early: Validate models as soon as they're created:
1 2 3 4 5 6 |
|
- Use Model Validation: Leverage Pydantic's validation features:
1 2 3 4 5 6 7 |
|
- Handle Serialization: Use proper serialization methods:
1 2 3 4 5 6 |
|
Performance Notes
- All models use Pydantic v2 for optimal performance and validation
- Models support both dictionary and JSON serialization/deserialization
- Validation is performed at object creation time, not during access
- Use
model_dump()
for dictionary representation andmodel_dump_json()
for JSON strings - Large schemas with many custom properties are efficiently handled through lazy validation
See Also
- Pydantic Documentation - For detailed information about Pydantic features
- JSON Schema Specification - For understanding JSON schema standards
- RDE Documentation - For RDE-specific schema requirements