Generates a JSON schema that matches a dict schema. Args: schema: The core schema. Returns: The generated JSON schema.
(self, schema: core_schema.DictSchema)
| 1068 | return json_schema |
| 1069 | |
| 1070 | def dict_schema(self, schema: core_schema.DictSchema) -> JsonSchemaValue: |
| 1071 | """Generates a JSON schema that matches a dict schema. |
| 1072 | |
| 1073 | Args: |
| 1074 | schema: The core schema. |
| 1075 | |
| 1076 | Returns: |
| 1077 | The generated JSON schema. |
| 1078 | """ |
| 1079 | json_schema: JsonSchemaValue = {'type': 'object'} |
| 1080 | |
| 1081 | keys_schema = self.generate_inner(schema['keys_schema']).copy() if 'keys_schema' in schema else {} |
| 1082 | if '$ref' not in keys_schema: |
| 1083 | keys_pattern = keys_schema.pop('pattern', None) |
| 1084 | # Don't give a title to patternProperties/propertyNames: |
| 1085 | keys_schema.pop('title', None) |
| 1086 | else: |
| 1087 | # Here, we assume that if the keys schema is a definition reference, |
| 1088 | # it can't be a simple string core schema (and thus no pattern can exist). |
| 1089 | # However, this is only in practice (in theory, a definition reference core |
| 1090 | # schema could be generated for a simple string schema). |
| 1091 | # Note that we avoid calling `self.resolve_ref_schema`, as it might not exist yet. |
| 1092 | keys_pattern = None |
| 1093 | |
| 1094 | values_schema = self.generate_inner(schema['values_schema']).copy() if 'values_schema' in schema else {} |
| 1095 | # don't give a title to additionalProperties: |
| 1096 | values_schema.pop('title', None) |
| 1097 | |
| 1098 | if values_schema or keys_pattern is not None: |
| 1099 | if keys_pattern is None: |
| 1100 | json_schema['additionalProperties'] = values_schema |
| 1101 | else: |
| 1102 | json_schema['patternProperties'] = {keys_pattern: values_schema} |
| 1103 | else: # for `dict[str, Any]`, we allow any key and any value, since `str` is the default key type |
| 1104 | json_schema['additionalProperties'] = True |
| 1105 | |
| 1106 | if ( |
| 1107 | # The len check indicates that constraints are probably present: |
| 1108 | (keys_schema.get('type') == 'string' and len(keys_schema) > 1) |
| 1109 | # If this is a definition reference schema, it most likely has constraints: |
| 1110 | or '$ref' in keys_schema |
| 1111 | ): |
| 1112 | keys_schema.pop('type', None) |
| 1113 | json_schema['propertyNames'] = keys_schema |
| 1114 | |
| 1115 | self.update_with_validations(json_schema, schema, self.ValidationsMapping.object) |
| 1116 | return json_schema |
| 1117 | |
| 1118 | def function_before_schema(self, schema: core_schema.BeforeValidatorFunctionSchema) -> JsonSchemaValue: |
| 1119 | """Generates a JSON schema that matches a function-before schema. |