diff --git a/auth.go b/auth.go index 516a0187..53afa3e5 100644 --- a/auth.go +++ b/auth.go @@ -36,29 +36,29 @@ func password(f *Faker, lower bool, upper bool, numeric bool, special bool, spac items := make([]any, 0) weights := make([]float32, 0) if lower { - items = append(items, "lower") + items = append(items, "l") weights = append(weights, 4) } if upper { - items = append(items, "upper") + items = append(items, "u") weights = append(weights, 4) } if numeric { - items = append(items, "numeric") + items = append(items, "n") weights = append(weights, 3) } if special { - items = append(items, "special") + items = append(items, "e") weights = append(weights, 2) } if space { - items = append(items, "space") + items = append(items, "a") weights = append(weights, 1) } // If no items are selected then default to lower, upper, numeric if len(items) == 0 { - items = append(items, "lower", "upper", "numeric") + items = append(items, "l", "u", "n") weights = append(weights, 4, 4, 3) } @@ -70,15 +70,15 @@ func password(f *Faker, lower bool, upper bool, numeric bool, special bool, spac weight, _ := weighted(f, items, weights) switch weight.(string) { - case "lower": + case "l": b[i] = lowerStr[f.Int64()%int64(len(lowerStr))] - case "upper": + case "u": b[i] = upperStr[f.Int64()%int64(len(upperStr))] - case "numeric": + case "n": b[i] = numericStr[f.Int64()%int64(len(numericStr))] - case "special": + case "e": b[i] = specialSafeStr[f.Int64()%int64(len(specialSafeStr))] - case "space": + case "a": b[i] = spaceStr[f.Int64()%int64(len(spaceStr))] } } diff --git a/auth_test.go b/auth_test.go index c1f91425..1e78a56d 100644 --- a/auth_test.go +++ b/auth_test.go @@ -61,8 +61,8 @@ func ExamplePassword() { // Output: cfelntbponnbbzrhswobuwlxajeeclrx // KYEKNGUUNKUYSFBUFFTGDKUVCVYKPONP // 43622637275953627791234759581343 - // !?*-_-?#@!*?@?-&@!*!*?_!#$@@@**@ - // .DTHJ&@oF#d.L5F65 N.-#U5xWX F0DI + // @.__-._-!-!_..!-_*_*__-@*.__.__! + // -DTHJ@.oF@d@L5F65 N-.@U5xWX F0DI // foZnB } @@ -78,8 +78,8 @@ func ExampleFaker_Password() { // Output: cfelntbponnbbzrhswobuwlxajeeclrx // KYEKNGUUNKUYSFBUFFTGDKUVCVYKPONP // 43622637275953627791234759581343 - // !?*-_-?#@!*?@?-&@!*!*?_!#$@@@**@ - // .DTHJ&@oF#d.L5F65 N.-#U5xWX F0DI + // @.__-._-!-!_..!-_*_*__-@*.__.__! + // -DTHJ@.oF@d@L5F65 N-.@U5xWX F0DI // foZnB } diff --git a/faker_test.go b/faker_test.go index 6b5c191f..a3ea787c 100644 --- a/faker_test.go +++ b/faker_test.go @@ -34,7 +34,7 @@ func Example() { // Credit Card: 6282690620525711 // Hacker Phrase: Try to bundle the PNG firewall, maybe it will deconstruct the open-source bandwidth! // Job Title: Assistant - // Password: Nyf8p8ka1Kvgn**@3H&$w7j01yM1vkc2 + // Password: Nyf8p8ka1Kvgn...3H*.w7j01yM1vkc2 } func ExampleNew() { diff --git a/helpers.go b/helpers.go index 52c62e78..6773e39f 100644 --- a/helpers.go +++ b/helpers.go @@ -15,7 +15,7 @@ const lowerStr = "abcdefghijklmnopqrstuvwxyz" const upperStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" const numericStr = "0123456789" const specialStr = "@#$%&?|!(){}<>=*+-_:;,." -const specialSafeStr = "@#$&?!-_*." +const specialSafeStr = "!@.-_*" // https://github.com/1Password/spg/pull/22 const spaceStr = " " const allStr = lowerStr + upperStr + numericStr + specialStr + spaceStr const vowels = "aeiou" diff --git a/json.go b/json.go index 5b2ef045..858e5233 100644 --- a/json.go +++ b/json.go @@ -254,7 +254,19 @@ func addFileJSONLookup() { // encoding/json.RawMessage is a special case of []byte // it cannot be handled as a reflect.Array/reflect.Slice // because it needs additional structure in the output -func rJsonRawMessage(f *Faker, v reflect.Value) error { +func rJsonRawMessage(f *Faker, v reflect.Value, tag string) error { + if tag != "" { + err := rCustom(f, v, tag) + if err == nil { + jsonData := v.Bytes() + if !json.Valid(jsonData) { + fName, _ := parseNameAndParamsFromTag(tag) + return errors.New("custom function " + fName + " returned invalid json data: " + string(jsonData)) + } + } + return err + } + b, err := f.JSON(nil) if err != nil { return err diff --git a/json_test.go b/json_test.go index 5a2bcb01..02b33df1 100644 --- a/json_test.go +++ b/json_test.go @@ -395,6 +395,84 @@ func TestJSONRawMessageWithTag(t *testing.T) { } } +func TestJSONRawMessageWithCustomFuncTag(t *testing.T) { + AddFuncLookup("customjsontest", Info{ + Display: "CustomJSONTest", + Category: "file", + Example: `{"ErTA":"bale","FQJUIGrmnRBfuGlb":"over","HTJJPnEKGS":"please say that again","HvLvfsQRGbK":"whenever one turns around","KKbMlbxquDmwwvRWVlPmwRAeAw":"Voluptatem eaque quia facilis quo."}`, + Output: "[]byte", + ContentType: "application/json", + Params: []Param{}, + Generate: func(f *Faker, m *MapParams, info *Info) (any, error) { + length := f.Number(5, 15) + dataSet := []string{"word", "phrase", "loremipsumsentence", "{hackeradjective}-{hackernoun}", "float64", "bool"} + resultMap := make(map[string]any) + for i := 0; i < length; i++ { + key := f.LetterN(uint(f.Number(3, 30))) + val, err := f.Generate(RandomString(dataSet)) + if err != nil { + return nil, err + } + resultMap[key] = val + } + marshal, err := json.Marshal(resultMap) + if err != nil { + panic(err) + } + return marshal, nil + }, + }) + + type J struct { + Field json.RawMessage `json:"field" fake:"customjsontest"` + } + + Seed(11) + + var objs []J + Slice(&objs) + + _, err := json.Marshal(objs) + if err != nil { + t.Fatal(err) + } +} + +func TestJSONRawMessageWithInvalidCustomFuncTag(t *testing.T) { + AddFuncLookup("invalidjsontest", Info{ + Display: "InvalidJSONTest", + Category: "file", + Example: `[181 251 51 164 185 142 21 3 33]`, + Output: "[]byte", + Params: []Param{}, + Generate: func(f *Faker, m *MapParams, info *Info) (any, error) { + var result []byte + Slice(&result) + return result, nil + }, + }) + + type J struct { + Field json.RawMessage `json:"field" fake:"invalidjsontest"` + } + + Seed(11) + + var objs []J + Slice(&objs) + + _, err := json.Marshal(objs) + if err != nil { + t.Fatal(err) + } + + var obj J + err = Struct(&obj) + if err == nil { + t.Fatal("'Struct(&obj)' was supposed to return an error but didn't") + } +} + func TestJSONNumber(t *testing.T) { type J struct { Field json.Number `json:"field"` diff --git a/struct.go b/struct.go index b203959d..b2ac99a3 100644 --- a/struct.go +++ b/struct.go @@ -39,7 +39,7 @@ func r(f *Faker, t reflect.Type, v reflect.Value, tag string, size int) error { switch t.Name() { case "RawMessage": - return rJsonRawMessage(f, v) + return rJsonRawMessage(f, v, tag) case "Number": return rJsonNumber(f, v, tag) default: