JSONSchemaForResourceType returns a JSON schema for validating the properties of a given resource type. Note: You can use ParseResourceKind to get the ResourceKind from a string.
(resourceType ResourceKind)
| 57 | // JSONSchemaForResourceType returns a JSON schema for validating the properties of a given resource type. |
| 58 | // Note: You can use ParseResourceKind to get the ResourceKind from a string. |
| 59 | func JSONSchemaForResourceType(resourceType ResourceKind) (*jsonschema.Schema, error) { |
| 60 | // Ensure the schema is parsed |
| 61 | parsedResourceYAMLSchemaOnce.Do(func() { |
| 62 | var err error |
| 63 | parsedResourceYAMLSchema, err = parseSchemaFromYAML(resourceYAMLSchema) |
| 64 | if err != nil { |
| 65 | panic(fmt.Sprintf("failed to parse schema: %v", err)) |
| 66 | } |
| 67 | }) |
| 68 | |
| 69 | // Look up the definition key for this resource type |
| 70 | defKey, ok := resourceKindToDefinitionKey[resourceType] |
| 71 | if !ok { |
| 72 | return nil, fmt.Errorf("no schema definition for resource type %v", resourceType) |
| 73 | } |
| 74 | |
| 75 | // Get the definition from the schema |
| 76 | defSchema, ok := parsedResourceYAMLSchema.Definitions[defKey] |
| 77 | if !ok { |
| 78 | return nil, fmt.Errorf("schema definition %q not found", defKey) |
| 79 | } |
| 80 | |
| 81 | return defSchema, nil |
| 82 | } |
| 83 | |
| 84 | // parseSchemaFromYAML parses a JSON schema from YAML content. |
| 85 | func parseSchemaFromYAML(yamlContent string) (*jsonschema.Schema, error) { |