func translateType(typ SimpleType) (*relapse.Pattern, error) { switch typ { case TypeArray, TypeObject: //This does not distinguish between arrays and objects return combinator.Many(combinator.InAny(combinator.Any())), nil case TypeBoolean: return combinator.Value(funcs.TypeBool(funcs.BoolVar())), nil case TypeInteger: return combinator.Value(funcs.TypeDouble(Integer())), nil case TypeNull: //TODO null is not being returned by json parser, but is also not empty return combinator.Value(funcs.Not( funcs.Or( funcs.TypeDouble(Number()), funcs.Or( funcs.TypeBool(funcs.BoolVar()), funcs.TypeString(funcs.StringVar()), ), ), )), nil case TypeNumber: return combinator.Value(funcs.TypeDouble(Number())), nil case TypeString: return combinator.Value(funcs.TypeString(funcs.StringVar())), nil } panic(fmt.Sprintf("unknown simpletype: %s", typ)) }
func translateString(schema String) (*relapse.Pattern, error) { v := funcs.StringVar() list := []funcs.Bool{} notStr := funcs.Not(funcs.TypeString(funcs.StringVar())) if schema.MaxLength != nil { ml := MaxLength(v, int64(*schema.MaxLength)) list = append(list, funcs.Or(ml, notStr)) } if schema.MinLength > 0 { ml := MinLength(v, int64(schema.MinLength)) list = append(list, funcs.Or(ml, notStr)) } if schema.Pattern != nil { p := funcs.Regex(funcs.StringConst(*schema.Pattern), v) list = append(list, funcs.Or(p, notStr)) } if len(list) == 0 { return combinator.Value(funcs.TypeString(v)), nil } return combinator.Value(and(list)), nil }