示例#1
0
文件: all_test.go 项目: rs/rest-layer
func TestErrNotImplemented(t *testing.T) {
	s := schema.Schema{
		Fields: schema.Fields{
			"i": {
				Validator: &dummyValidator{},
			},
		},
	}
	enc := jsonschema.NewEncoder(new(bytes.Buffer))
	assert.Equal(t, jsonschema.ErrNotImplemented, enc.Encode(&s))
}
示例#2
0
func TestStringValidatorNoBoundaryPanic(t *testing.T) {
	s := schema.Schema{
		Fields: schema.Fields{
			"s": {
				Validator: &schema.String{},
			},
		},
	}
	assert.NotPanics(t, func() {
		enc := jsonschema.NewEncoder(new(bytes.Buffer))
		enc.Encode(&s)
	})
}
示例#3
0
文件: all_test.go 项目: rs/rest-layer
func (tc *encoderTestCase) Run(t *testing.T) {
	t.Run(tc.name, func(t *testing.T) {
		t.Parallel()

		b := new(bytes.Buffer)
		enc := jsonschema.NewEncoder(b)
		assert.NoError(t, enc.Encode(&tc.schema))

		if tc.customValidate == nil {
			assert.JSONEq(t, tc.expect, b.String())
		} else {
			tc.customValidate(t, b.Bytes())
		}
	})
}
示例#4
0
func ExampleEncoder() {
	s := schema.Schema{
		Fields: schema.Fields{
			"foo": schema.Field{
				Required: true,
				// NOTE: Min is currently encoded as '0E+00', not '0'.
				Validator: &schema.Float{Boundaries: &schema.Boundaries{Min: 0, Max: math.Inf(1)}},
			},
			"bar": schema.Field{
				Validator: &schema.Integer{},
			},
			"baz": schema.Field{
				ReadOnly:  true,
				Validator: &schema.String{MaxLen: 42},
			},
			"foobar": schema.Field{},
		},
	}
	b := new(bytes.Buffer)
	enc := jsonschema.NewEncoder(b)
	enc.Encode(&s)
	b2 := new(bytes.Buffer)
	json.Indent(b2, b.Bytes(), "", "| ")
	fmt.Println(b2)
	// Output: {
	// | "type": "object",
	// | "additionalProperties": false,
	// | "properties": {
	// | | "bar": {
	// | | | "type": "integer"
	// | | },
	// | | "baz": {
	// | | | "readOnly": true,
	// | | | "type": "string",
	// | | | "maxLength": 42
	// | | },
	// | | "foo": {
	// | | | "type": "number",
	// | | | "minimum": 0E+00
	// | | },
	// | | "foobar": {}
	// | },
	// | "required": [
	// | | "foo"
	// | ]
	// }
}