本文整理汇总了Golang中github.com/cloudwan/gohan/schema.Schema.ValidateOnCreate方法的典型用法代码示例。如果您正苦于以下问题:Golang Schema.ValidateOnCreate方法的具体用法?Golang Schema.ValidateOnCreate怎么用?Golang Schema.ValidateOnCreate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudwan/gohan/schema.Schema
的用法示例。
在下文中一共展示了Schema.ValidateOnCreate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CreateResource
// CreateResource creates the resource specified by the schema and dataMap
func CreateResource(
context middleware.Context,
dataStore db.DB,
identityService middleware.IdentityService,
resourceSchema *schema.Schema,
dataMap map[string]interface{},
) error {
manager := schema.GetManager()
// Load environment
environmentManager := extension.GetManager()
environment, ok := environmentManager.GetEnvironment(resourceSchema.ID)
if !ok {
return fmt.Errorf("No environment for schema")
}
auth := context["auth"].(schema.Authorization)
//LoadPolicy
policy, err := loadPolicy(context, "create", resourceSchema.GetPluralURL(), auth)
if err != nil {
return err
}
_, err = resourceSchema.GetPropertyByID("tenant_id")
if _, ok := dataMap["tenant_id"]; err == nil && !ok {
dataMap["tenant_id"] = context["tenant_id"]
}
if tenantID, ok := dataMap["tenant_id"]; ok && tenantID != nil {
dataMap["tenant_name"], err = identityService.GetTenantName(tenantID.(string))
if err != nil {
return ResourceError{err, err.Error(), Unauthorized}
}
}
//Apply policy for api input
err = policy.Check(schema.ActionCreate, auth, dataMap)
if err != nil {
return ResourceError{err, err.Error(), Unauthorized}
}
delete(dataMap, "tenant_name")
// apply property filter
err = policy.ApplyPropertyConditionFilter(schema.ActionCreate, dataMap, nil)
if err != nil {
return ResourceError{err, err.Error(), Unauthorized}
}
context["resource"] = dataMap
if id, ok := dataMap["id"]; !ok || id == "" {
dataMap["id"] = uuid.NewV4().String()
}
context["id"] = dataMap["id"]
if err := extension.HandleEvent(context, environment, "pre_create"); err != nil {
return err
}
if resourceData, ok := context["resource"].(map[string]interface{}); ok {
dataMap = resourceData
}
//Validation
err = resourceSchema.ValidateOnCreate(dataMap)
if err != nil {
return ResourceError{err, fmt.Sprintf("Validation error: %s", err), WrongData}
}
resource, err := manager.LoadResource(resourceSchema.ID, dataMap)
if err != nil {
return err
}
//Fillup default
err = resource.PopulateDefaults()
if err != nil {
return err
}
context["resource"] = resource.Data()
if err := InTransaction(
context, dataStore,
transaction.GetIsolationLevel(resourceSchema, schema.ActionCreate),
func() error {
return CreateResourceInTransaction(context, resource)
},
); err != nil {
return err
}
if err := extension.HandleEvent(context, environment, "post_create"); err != nil {
return err
}
if err := ApplyPolicyForResource(context, resourceSchema); err != nil {
return ResourceError{err, "", Unauthorized}
}
return nil
}