示例#1
0
func translateNumeric(schema Numeric) (*relapse.Pattern, error) {
	v := Number()
	list := []funcs.Bool{}
	notNum := funcs.Not(funcs.TypeDouble(Number()))
	if schema.MultipleOf != nil {
		mult := MultipleOf(v, funcs.DoubleConst(*schema.MultipleOf))
		list = append(list, funcs.Or(mult, notNum))
	}
	if schema.Maximum != nil {
		lt := funcs.DoubleLE(v, funcs.DoubleConst(*schema.Maximum))
		if schema.ExclusiveMaximum {
			lt = funcs.DoubleLt(v, funcs.DoubleConst(*schema.Maximum))
		}
		list = append(list, funcs.Or(lt, notNum))
	}
	if schema.Minimum != nil {
		lt := funcs.DoubleGE(v, funcs.DoubleConst(*schema.Minimum))
		if schema.ExclusiveMinimum {
			lt = funcs.DoubleGt(v, funcs.DoubleConst(*schema.Minimum))
		}
		list = append(list, funcs.Or(lt, notNum))
	}
	if len(list) == 0 {
		return combinator.Value(funcs.TypeDouble(v)), nil
	}
	return combinator.Value(and(list)), nil
}
示例#2
0
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))
}
示例#3
0
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
}