2024-03-12 13:50:13 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/santhosh-tekuri/jsonschema/v5"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//go:embed zitadel.schema.v1.json
|
|
|
|
zitadelJSON string
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MetaSchemaID = "urn:zitadel:schema:v1"
|
|
|
|
)
|
|
|
|
|
2024-08-28 19:46:45 +00:00
|
|
|
func NewSchema(role Role, r io.Reader) (*jsonschema.Schema, error) {
|
2024-03-12 13:50:13 +00:00
|
|
|
c := jsonschema.NewCompiler()
|
|
|
|
if err := c.AddResource(PermissionSchemaID, strings.NewReader(permissionJSON)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := c.AddResource(MetaSchemaID, strings.NewReader(zitadelJSON)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.RegisterExtension(PermissionSchemaID, permissionSchema, permissionExtension{
|
|
|
|
role,
|
|
|
|
})
|
|
|
|
if err := c.AddResource("schema.json", r); err != nil {
|
2024-08-28 19:46:45 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(err, "COMMA-Frh42", "Errors.UserSchema.Invalid")
|
2024-03-12 13:50:13 +00:00
|
|
|
}
|
|
|
|
schema, err := c.Compile("schema.json")
|
|
|
|
if err != nil {
|
2024-08-28 19:46:45 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(err, "COMMA-W21tg", "Errors.UserSchema.Invalid")
|
2024-03-12 13:50:13 +00:00
|
|
|
}
|
|
|
|
return schema, nil
|
|
|
|
}
|