Skip to content

Commit eed9710

Browse files
committed
feat: configurable exclude fields
1 parent 554acad commit eed9710

File tree

9 files changed

+108
-5
lines changed

9 files changed

+108
-5
lines changed

lib/anchor/json_schema/resource.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module Anchor::JSONSchema
22
class Resource < Anchor::Resource
3-
def express(context: {}, include_all_fields:)
3+
def express(context: {}, include_all_fields:, exclude_fields:)
44
included_fields = schema_fetchable_fields(context:, include_all_fields:)
5+
included_fields -= exclude_fields if exclude_fields
56

67
properties = [id_property, type_property] +
78
Array.wrap(anchor_attributes_properties(included_fields:)) +

lib/anchor/json_schema/schema_generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def definitions
3434
resource.anchor_schema_name => type_property(resource.express(
3535
context: @context,
3636
include_all_fields: @include_all_fields,
37+
exclude_fields: @exclude_fields.nil? ? [] : @exclude_fields[r.anchor_schema_name.to_sym],
3738
)),
3839
}
3940
end.reduce(&:merge)

lib/anchor/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def enum(enum)
1313
@enums.push(enum)
1414
end
1515

16-
def generate(context: {}, adapter: :type_script, include_all_fields: false)
16+
def generate(context: {}, adapter: :type_script, include_all_fields: false, exclude_fields: nil)
1717
adapter = case adapter
1818
when :type_script then Anchor::TypeScript::SchemaGenerator
1919
when :json_schema then Anchor::JSONSchema::SchemaGenerator
@@ -24,6 +24,7 @@ def generate(context: {}, adapter: :type_script, include_all_fields: false)
2424
register: Register.new(resources: @resources || [], enums: @enums || []),
2525
context:,
2626
include_all_fields:,
27+
exclude_fields:,
2728
)
2829
end
2930
end

lib/anchor/schema_generator.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module Anchor
22
class SchemaGenerator
3-
def initialize(register:, context:, include_all_fields:)
3+
def initialize(register:, context:, include_all_fields:, exclude_fields:)
44
@register = register
55
@context = context
66
@include_all_fields = include_all_fields
7+
@exclude_fields = exclude_fields
78
end
89

910
def self.call(...)

lib/anchor/type_script/resource.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module Anchor::TypeScript
22
class Resource < Anchor::Resource
3-
def express(context: {}, include_all_fields:)
3+
def express(context: {}, include_all_fields:, exclude_fields:)
44
included_fields = schema_fetchable_fields(context:, include_all_fields:)
5+
included_fields -= exclude_fields if exclude_fields
56

67
properties = [id_property, type_property] +
78
Array.wrap(anchor_attributes_properties(included_fields:)) +

lib/anchor/type_script/schema_generator.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ def call
44
maybe_type = "type Maybe<T> = T | null;"
55

66
enum_expressions = enums.map(&:express)
7-
type_expressions = resources.map { |r| r.express(context: @context, include_all_fields: @include_all_fields) }
7+
type_expressions = resources.map do |r|
8+
r.express(
9+
context: @context,
10+
include_all_fields: @include_all_fields,
11+
exclude_fields: @exclude_fields.nil? ? [] : @exclude_fields[r.anchor_schema_name.to_sym],
12+
)
13+
end
814

915
([maybe_type] + enum_expressions + type_expressions).join("\n\n") + "\n"
1016
end

spec/anchor/example_schema_snapshot_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def self.snapshot_test(filename, generate)
1919
snapshot_test "schema.ts", -> { Schema.generate(include_all_fields: true) }
2020
snapshot_test "test_schema.ts", -> { Schema.generate(context: { role: "test" }) }
2121
snapshot_test "all_fields_false_schema.ts", -> { Schema.generate }
22+
snapshot_test "excluded_fields_schema.ts", -> { Schema.generate(exclude_fields: { User: [:name, :posts] }) }
2223
snapshot_test "json_schema.json", -> { Schema.generate(adapter: :json_schema, include_all_fields: true) }
2324
end
2425
# rubocop:enable RSpec/EmptyExampleGroup

spec/example/lib/tasks/anchor.rake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace :anchor do
1212
write_to "schema.ts", -> { Schema.generate(include_all_fields: true) }
1313
write_to "test_schema.ts", -> { Schema.generate(context: { role: "test" }) }
1414
write_to "all_fields_false_schema.ts", -> { Schema.generate }
15+
write_to "excluded_fields_schema.ts", -> {
16+
Schema.generate(exclude_fields: { User: [:name, :posts] })
17+
}
1518
write_to "json_schema.json", -> { Schema.generate(adapter: :json_schema, include_all_fields: true) }
1619
end
1720
end
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
type Maybe<T> = T | null;
2+
3+
export enum UserRole {
4+
Admin = "admin",
5+
ContentCreator = "content_creator",
6+
External = "external",
7+
Guest = "guest",
8+
System = "system",
9+
}
10+
11+
export type Comment = {
12+
id: number;
13+
type: "comments";
14+
createdAt: string;
15+
};
16+
17+
export type User = {
18+
id: number;
19+
type: "users";
20+
role: UserRole;
21+
relationships: {
22+
comments: Array<Comment>;
23+
};
24+
};
25+
26+
export type Post = {
27+
id: number;
28+
type: "posts";
29+
description: string;
30+
relationships: {
31+
user: User;
32+
comments: Array<Comment>;
33+
participants: Array<User>;
34+
};
35+
};
36+
37+
export type Exhaustive = {
38+
id: number;
39+
type: "exhaustives";
40+
/** My asserted string. */
41+
assertedString: string;
42+
assertedNumber: number;
43+
assertedBoolean: boolean;
44+
assertedNull: null;
45+
assertedUnknown: unknown;
46+
assertedObject: {
47+
a: "a";
48+
"b-dash": 1;
49+
c: Maybe<string>;
50+
d_optional?: Maybe<string>;
51+
};
52+
assertedMaybeObject: Maybe<{
53+
a: "a";
54+
"b-dash": 1;
55+
c: Maybe<string>;
56+
d_optional?: Maybe<string>;
57+
}>;
58+
assertedArrayRecord: Array<Record<string, number>>;
59+
assertedUnion: string | number;
60+
/** This is a provided description. */
61+
withDescription: string;
62+
inferredUnknown: unknown;
63+
uuid: string;
64+
string: string;
65+
maybeString: string;
66+
text: string;
67+
integer: number;
68+
float: number;
69+
decimal: string;
70+
datetime: string;
71+
timestamp: string;
72+
time: string;
73+
date: string;
74+
boolean: boolean;
75+
arrayString: Array<string>;
76+
maybeArrayString: Maybe<Array<string>>;
77+
json: Record<string, unknown>;
78+
jsonb: Record<string, unknown>;
79+
daterange: unknown;
80+
enum: unknown;
81+
virtualUpcasedString: Maybe<string>;
82+
loljk: "never";
83+
delegatedMaybeString: string;
84+
modelOverridden: unknown;
85+
resourceOverridden: unknown;
86+
/** This is a comment. */
87+
withComment: Maybe<string>;
88+
};

0 commit comments

Comments
 (0)