| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| package schema |
|
|
| import ( |
| "fmt" |
| "regexp" |
| ) |
|
|
| var ( |
| validateClassNameRegex = regexp.MustCompile(`^` + ClassNameRegexCore + `$`) |
| validateTenantNameRegex = regexp.MustCompile(`^` + ShardNameRegexCore + `$`) |
| validatePropertyNameRegex = regexp.MustCompile(`^` + PropertyNameRegex + `$`) |
| validateNestedPropertyNameRegex = regexp.MustCompile(`^` + NestedPropertyNameRegex + `$`) |
| reservedPropertyNames = []string{"_additional", "_id", "id"} |
| ) |
|
|
| const ( |
| |
| |
| classNameMaxLength = 255 |
| ClassNameRegexCore = `[A-Z][_0-9A-Za-z]{0,254}` |
| |
| ClassNameRegexAllowRegex = `^(\*|[A-Z][_0-9A-Za-z\-.*+?^$()|{}\[\]\\]{0,254})$` |
| |
| ShardNameRegexCore = `[A-Za-z0-9\-\_]{1,64}` |
| |
| ShardNameRegexAllowRegex = `^[A-Za-z0-9\-_.*+?^$()|{}\[\]\\*]{1,64}$` |
| |
| |
| |
| |
| |
| |
| |
| |
| propertyNameMaxLength = 231 |
| PropertyNameRegex = `[_A-Za-z][_0-9A-Za-z]{0,230}` |
| |
| |
| NestedPropertyNameRegex = `[_A-Za-z][_0-9A-Za-z]*` |
| |
| TargetVectorNameMaxLength = 230 |
| TargetVectorNameRegex = `[_A-Za-z][_0-9A-Za-z]{0,229}` |
| ) |
|
|
| |
| func ValidateClassName(name string) (ClassName, error) { |
| c, err := validateClassOrAliasName(name, false) |
| if err != nil { |
| return "", err |
| } |
| return ClassName(c), nil |
| } |
|
|
| func ValidateAliasName(name string) (string, error) { |
| return validateClassOrAliasName(name, true) |
| } |
|
|
| |
| |
| func ValidateClassNameIncludesRegex(name string) (ClassName, error) { |
| if len(name) > classNameMaxLength { |
| return "", fmt.Errorf("'%s' is not a valid class name. Name should not be longer than %d characters", |
| name, classNameMaxLength) |
| } |
| if !regexp.MustCompile(ClassNameRegexAllowRegex).MatchString(name) { |
| return "", fmt.Errorf("'%s' is not a valid class name", name) |
| } |
| return ClassName(name), nil |
| } |
|
|
| func validateClassOrAliasName(name string, isAlias bool) (string, error) { |
| typ := "class" |
| if isAlias { |
| typ = "alias" |
| } |
|
|
| if len(name) > classNameMaxLength { |
| return "", fmt.Errorf("'%s' is not a valid %s name. Name should not be longer than %d characters", |
| name, typ, classNameMaxLength) |
| } |
| if !validateClassNameRegex.MatchString(name) { |
| return "", fmt.Errorf("'%s' is not a valid %s name", name, typ) |
| } |
| return name, nil |
| } |
|
|
| |
| func ValidateTenantName(name string) error { |
| if !validateTenantNameRegex.MatchString(name) { |
| var msg string |
| if name == "" { |
| msg = "empty tenant name" |
| } else { |
| msg = fmt.Sprintf( |
| " '%s' is not a valid tenant name. should only contain alphanumeric characters (a-z, A-Z, 0-9), "+ |
| "underscore (_), and hyphen (-), with a length between 1 and 64 characters", |
| name, |
| ) |
| } |
| return fmt.Errorf("%s", msg) |
| } |
| return nil |
| } |
|
|
| |
| |
| func ValidateTenantNameIncludesRegex(name string) error { |
| if !regexp.MustCompile(ShardNameRegexAllowRegex).MatchString(name) { |
| var msg string |
| if name == "" { |
| msg = "empty tenant name" |
| } else { |
| msg = fmt.Sprintf( |
| " '%s' is not a valid tenant name. should only contain alphanumeric characters (a-z, A-Z, 0-9), "+ |
| "underscore (_), and hyphen (-), with a length between 1 and 64 characters", |
| name, |
| ) |
| } |
| return fmt.Errorf("%s", msg) |
| } |
| return nil |
| } |
|
|
| |
| func ValidatePropertyName(name string) (PropertyName, error) { |
| if len(name) > propertyNameMaxLength { |
| return "", fmt.Errorf("'%s' is not a valid property name. Name should not be longer than %d characters", |
| name, propertyNameMaxLength) |
| } |
| if !validatePropertyNameRegex.MatchString(name) { |
| return "", fmt.Errorf("'%s' is not a valid property name. "+ |
| "Property names in Weaviate are restricted to valid GraphQL names, "+ |
| "which must be “/%s/”", name, PropertyNameRegex) |
| } |
| return PropertyName(name), nil |
| } |
|
|
| |
| func ValidateNestedPropertyName(name, prefix string) error { |
| if !validateNestedPropertyNameRegex.MatchString(name) { |
| return fmt.Errorf("'%s' is not a valid nested property name of '%s'. "+ |
| "NestedProperty names in Weaviate are restricted to valid GraphQL names, "+ |
| "which must be “/%s/”", name, prefix, NestedPropertyNameRegex) |
| } |
| return nil |
| } |
|
|
| |
| func ValidateReservedPropertyName(name string) error { |
| for i := range reservedPropertyNames { |
| if name == reservedPropertyNames[i] { |
| return fmt.Errorf("'%s' is a reserved property name", name) |
| } |
| } |
| return nil |
| } |
|
|
| |
| |
| func AssertValidClassName(name string) ClassName { |
| n, err := ValidateClassName(name) |
| if err != nil { |
| panic(fmt.Sprintf("Did not expect to be handled '%s', an invalid class name", name)) |
| } |
| return n |
| } |
|
|
| |
| |
| func AssertValidPropertyName(name string) PropertyName { |
| n, err := ValidatePropertyName(name) |
| if err != nil { |
| panic(fmt.Sprintf("Did not expect to be handled '%s', an invalid property name", name)) |
| } |
| return n |
| } |
|
|