Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions tools/spectral/ipa/__tests__/IPA111EffectiveFieldsReadOnly.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-111-effective-fields-read-only', [
{
name: 'valid effective field with readOnly: true',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
effectiveState: { type: 'string', readOnly: true },
},
},
},
},
},
errors: [],
},
{
name: 'valid non-effective field without readOnly',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
state: { type: 'string' },
effective: { type: 'string' },
},
},
},
},
},
errors: [],
},
{
name: 'invalid effective field without readOnly',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
effectiveState: { type: 'string' },
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-111-effective-fields-read-only',
message: 'Effective-value fields must be marked as readOnly: true.',
path: ['components', 'schemas', 'Schema', 'properties', 'effectiveState'],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'invalid effective field with readOnly: false',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
effectiveState: { type: 'string', readOnly: false },
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-111-effective-fields-read-only',
message: 'Effective-value fields must be marked as readOnly: true.',
path: ['components', 'schemas', 'Schema', 'properties', 'effectiveState'],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'invalid effective field in request schema',
document: {
paths: {
'/resources': {
post: {
requestBody: {
content: {
'application/vnd.atlas.2024-01-01+json': {
schema: {
type: 'object',
properties: {
effectiveState: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-111-effective-fields-read-only',
message: 'Effective-value fields represent server-computed state and must not appear in request schemas.',
path: [
'paths',
'/resources',
'post',
'requestBody',
'content',
'application/vnd.atlas.2024-01-01+json',
'schema',
'properties',
'effectiveState',
],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'invalid effective field without readOnly - exception',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
effectiveState: {
type: 'string',
'x-xgen-IPA-exception': {
'xgen-IPA-111-effective-fields-read-only': 'Reason',
},
},
},
},
},
},
},
errors: [],
},
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-111-optional-boolean-fields-default-false', [
{
name: 'valid optional boolean field with default false',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
enabled: { type: 'boolean', default: false },
},
},
},
},
},
errors: [],
},
{
name: 'valid required boolean field without default',
document: {
components: {
schemas: {
Schema: {
type: 'object',
required: ['enabled'],
properties: {
enabled: { type: 'boolean' },
},
},
},
},
},
errors: [],
},
{
name: 'valid non-boolean field without default',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
name: { type: 'string' },
},
},
},
},
},
errors: [],
},
{
name: 'invalid optional boolean field without default',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
enabled: { type: 'boolean' },
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-111-optional-boolean-fields-default-false',
message: 'Optional boolean fields must default to false.',
path: ['components', 'schemas', 'Schema', 'properties', 'enabled'],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'invalid optional boolean field with default true',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
enabled: { type: 'boolean', default: true },
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-111-optional-boolean-fields-default-false',
message: 'Optional boolean fields must default to false.',
path: ['components', 'schemas', 'Schema', 'properties', 'enabled'],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'invalid optional boolean field in request and response',
document: {
paths: {
'/resources': {
post: {
requestBody: {
content: {
'application/vnd.atlas.2024-01-01+json': {
schema: {
type: 'object',
properties: {
paused: { type: 'boolean' },
},
},
},
},
},
responses: {
201: {
content: {
'application/vnd.atlas.2024-01-01+json': {
schema: {
type: 'object',
properties: {
hidden: { type: 'boolean', default: true },
},
},
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-111-optional-boolean-fields-default-false',
message: 'Optional boolean fields must default to false.',
path: [
'paths',
'/resources',
'post',
'requestBody',
'content',
'application/vnd.atlas.2024-01-01+json',
'schema',
'properties',
'paused',
],
severity: DiagnosticSeverity.Error,
},
{
code: 'xgen-IPA-111-optional-boolean-fields-default-false',
message: 'Optional boolean fields must default to false.',
path: [
'paths',
'/resources',
'post',
'responses',
'201',
'content',
'application/vnd.atlas.2024-01-01+json',
'schema',
'properties',
'hidden',
],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'invalid optional boolean field without default - exception',
document: {
components: {
schemas: {
Schema: {
type: 'object',
properties: {
enabled: {
type: 'boolean',
'x-xgen-IPA-exception': {
'xgen-IPA-111-optional-boolean-fields-default-false': 'Reason',
},
},
},
},
},
},
},
errors: [],
},
]);
Loading
Loading